gpt4 book ai didi

Python "Guess your Number"错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:54:54 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com