Python:执行int函数时出现问题==

2024-06-16 13:18:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难让我的代码为我的游戏运行两个不同的选项(函数)。我创建了一个菜单功能选项,包括2个选项。你知道吗

但是,它返回错误“缺少2个必需的位置参数” 我该怎么解决这个问题?提前谢谢,很抱歉这是一个新手问题!你知道吗

def options(playerVsComputer, playerVsPlayer):
    playerN = input("How players are you?")
    if input == 1: playerVsComputer()
    if input == 2: playerVsPlayer()

options()

Tags: 函数代码功能游戏input参数ifdef
2条回答

首先,在python3.x中,input返回一个字符串以供使用。您试图将字符串与整数进行比较,而整数总是会为false。要将值用作整数,必须将结果转换为int。。。你知道吗

int(input("How players are you?"))

…或者您可以只比较字符串文字"1""2"。你知道吗

其次,没有使用正确的参数调用方法。这个方法需要两个参数(看起来像函数,因为你在过程中调用它们)。你知道吗

如果在别处声明了这些函数,则可以从函数中删除参数。否则,需要传入函数。你知道吗

在代码中,定义了一个有两个参数的函数,只能调用

options(1,2)

而且,看起来变量本身就是函数。 试试看

def options():
    playerN = int(input("How players are you?"))
    if playerN == 1: 
        playerVsComputer()
    if playerN == 2: 
        playerVsPlayer() 

并像上面定义的那样调用函数,不带参数。你知道吗

相关问题 更多 >