1

在书的帮助下,我正在 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

我迷路了,我不明白类似问题的答案。我将不胜感激代码的解决方案以及您可以提供的解释。谢谢您的帮助!

4

1 回答 1

1

您的代码认为您将三个参数添加到输入函数中(因为逗号),而它只需要一个。使用串联:

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

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

您还可以使用.format()

input("Are you thinking of {} ?".format(number2)) 
于 2013-07-06T02:42:17.460 回答