首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: inputNames()不接受任何参数(给定2个)

TypeError: inputNames()不接受任何参数(给定2个)
EN

Stack Overflow用户
提问于 2012-04-25 03:22:50
回答 1查看 5.9K关注 0票数 2
代码语言:javascript
复制
#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)。这条线是我的教授提供的,我想不出该怎么做。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2012-04-25 03:40:50

函数inputNames被定义为一个不带参数的函数,但是您在方法列表中向它传递了两个变量:

下面是你如何定义它的:

代码语言:javascript
复制
def inputNames():
    inputNames = string('Enter your names: ')
    return playerOne, playerTwo  

下面是你的名字:

代码语言:javascript
复制
playerOne, playerTwo = inputNames(playerOne, playerTwo)

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

代码语言:javascript
复制
playerOne, playerTwo = inputNames()

该函数必须在本地收集这两个名称,并返回这些名称,可能如下所示:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10304619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档