Python:TypeError:期望输入最多1个参数,得到5

问题描述 投票:1回答:5

[当我尝试输入将数字输入系统时,出现错误。

userInput= int(input("What is", RanNum1," - ", RanNum2," = ?"))

这是有问题的代码,该错误导致此:

Traceback (most recent call last):
File "H:/Project/MathProgram.py", line 144, in <module>
userAnswer = Addition(RanNum1, RanNum2)
File "H:/Project/MathProgram.py", line 33, in Addition
userInput= int(input("What is", RanNum1," - ", RanNum2," = ?"))
TypeError: input expected at most 1 arguments, got 5

我不知道为什么需要帮助!

python typeerror
5个回答
1
投票

这是五个要传递给input的参数:

input("What is", RanNum1, " - ", RanNum2, " = ?")

使用str.format方法提供单个字符串来输入。

inputstring = "What is {0} - {1} = ?".format(RanNum1, RanNum2)
userInput= int(input(inputstring))

4
投票

[input需要一个字符串,而不是5个参数。

您可以使用format函数使用这些变量来生成您的字符串。

userInput= int(input("What is {} - {} = ?".format(RanNum1, RanNum2))

1
投票

错误非常清楚...您正在传递5个参数:

userInput= int(input("What is", RanNum1," - ", RanNum2," = ?"))
                         ^^^      ^^^    ^^^     ^^^     ^^^
                          1        2      3       4       5

来自Java,是吗? :-)

[如果要在python中显示字符串中的变量,则可以查看string formatting(其他答案使用什么),也可以这样做:

userInput= int(input("What is %s - %s = ?" % (RanNum1, RanNum2)))

[此外,如果您正在使用Python 2.x(小于3的任何版本)运行此程序,我强烈建议您使用raw_input而不是input。输入将eval用户的输入作为python代码,然后eval is evil


0
投票

尽管print可以选择接受多个参数,但input不需要。

您需要使用format功能-userInput= int(input("What is {} - {} = ?".format(RanNum1, RanNum2)),或添加值userInput= int(input("What is" + str(RanNum1) + " - " + str(RanNum2) + " = ?"))(最好是第一个选项。


0
投票
userInput= int(input("What is", RanNum1," - ", RanNum2," = ?"))

# replace comma , with plus sign +

userInput= int(input("What is"+ RanNum1+" - "+ RanNum2+" = ?"))

# it is how i fixed when i got the same error message 

balance = float(input("OK, ", name, ". Please enter the cost of the", item, ":"))

# to

balance = float(input("OK, "+ name + ". Please enter the cost of the " + item + ": "))
热门问题
推荐问题
最新问题