Python“猜你的号码”错误

时间:2013-07-06 02:40:26

标签: python numbers

我正在猜测你的数字游戏(计算机猜测人类认为使用更高/更低输入的数字)在python的帮助下。我昨天开始学习python并且今天花了一整天寻求帮助但只是感到困惑。这是代码(到目前为止)和错误。

import random
print("Welcome to the Pick a Number Game!  Pick a number between 1 and 10 and I \n will guess it!")
number = random.randint (1, 10)
print("Are you thinking of", number,"?")
guess = input("Am I right on, higher, or lower? ")
if guess == "higher":
    number2 = random.randint (number, 10)
    input("Are you thinking of", number2,"?")    
elif guess == "lower":
    number3 = random.randint (1, number)
    input("Are you thinking of", number3,"?")                              
elif guess == "right on":
    print("I won!  Thanks for playing!")
    input("Press the enter key to exit.")

错误:

Traceback (most recent call last):
  File "C:\Users\Adam\Desktop\Number Challenge.py", line 8, in <module>
    input("Are you thinking of", number2,"?")
TypeError: input expected at most 1 arguments, got 3

我迷路了,我不明白类似问题的答案。我将很感激代码的解决方案和您可以给出的解释。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您的代码认为您在输入函数中添加了三个参数(因为逗号),只需要一个。使用连接:

input("Are you thinking of " + str(number2) + " ?") 

我们在这里调用str()将整数转换为字符串。我们不能用整数和字符串连接;它们必须是同一类型。

您还可以使用.format()

input("Are you thinking of {} ?".format(number2))