#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被定义为不带参数的函数,但是您要在方法列表中向其传递两个变量:

这是您的定义方式:

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

关于python - TypeError:inputNames()不接受任何参数(给定2个),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10304619/

10-16 08:47