2012-04-24 79 views
2
#Lab 7-3 The Dice Game 
#add libraries needed 
import random 

#the main function 
def main(): 
    print 

    #initiliaze variables 
    endProgram = 'no' 
    playerOne = 'NO NAME' 
    playerTwo = 'NO NAME' 

    #call to inputNames 
    playerOne, playerTwo = inputNames(playerOne, playerTwo) 

    #while loop to run program again 
    while endProgram == 'no': 
     winnersName = 'NO NAME' 
     p1number = 0 
     p2number = 0 

     #initiliaze variables 

     #call to rollDice 
     winnerName = rollDice(playerOne, playerTwo, winnerName) 

     #call to displayInfo 
     winnerName = displayInfo (winnerName) 

     endProgram = input('Do you want to end program?(Enter yes or no): ') 

#this function gets players names 
def inputNames(): 
    inputNames = string('Enter your names: ') 
    return playerOne, playerTwo  

#this function will get the random values 
def rollDice(): 
    p1number = random.randint(1,6) 
    p2number = random.randint(1,6) 
    if p1number >= p2number: 
     winnerName = playerOne 
    if p1number == p2numer: 
     winnerName = 'TIE' 
    elif winnerName == playerTwo: 
     return winnerName 

#this function displays the winner 
def displayInfo(): 
    print ('The winner is: ', winnerName) 


#calls main 
main() 

初学编程的位置,并试图完成的任务。第19行返回错误:TypeError:inputNames()不带参数(给定2)。第19行:playerOne,playerTwo = inputNames(playerOne,playerTwo)。这条线由我的教授提供,我无法弄清楚如何使它工作。任何帮助将不胜感激!类型错误:inputNames()不带任何参数(2给出)

+2

如果它是由你的教授给你......也许这就是_you_应该能够解决的一个暗示。也许这就是任务...... – 2012-04-24 19:27:20

回答

2

功能inputNames被定义为不接受参数的功能,但你传递给它的方法列表中两个变量:

这里是你如何定义它:

def inputNames(): 
    inputNames = string('Enter your names: ') 
    return playerOne, playerTwo 

这里是你如何叫做:

playerOne, playerTwo = inputNames(playerOne, playerTwo) 

你真正想要的是这个函数返回玩家一和玩家二的名字。所以上面的线确实应该:

playerOne, playerTwo = inputNames() 

和功能必须在本地收集两个名字和返回的,也许是这样的:

def inputNames(): 
    p1 = str(raw_input("Enter the name for player one: ")) 
    p2 = str(raw_input("Enter the name for player two: ")) 
    return p1, p2