Python "Guess your Number"错误

标签 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

我迷路了,我不明白类似问题的答案。我希望您能提供代码的解决方案和解释。感谢您的帮助!

最佳答案

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

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

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

您还可以使用.format():

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

关于Python "Guess your Number"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17499146/

相关文章:

python - 如何用Python将用户输入转换为列表?

python - 使用 Sklearn featurehasher

python - 使用一组固定的词汇随机生成解析树

numbers - 当分母与m不互质时如何计算 "modular multiplicative inverse"?

c++ - 如何在此处输出之前添加数字?

python - 如何在pygame中使用gfx

C# 计算循环中传递的数字数量

JavaScript 正则表达式仅匹配 X 位数字

java - 测试方法的返回类型是否为数字?

python - MySQL海量数据查询