我尝试使用此代码进行“猜数字”游戏:
import random
print("Pick any number between 1-100 and I'll try to guess it")
x = 1
y = 100
tries = 0
answer = "whatever"
while answer != "yes":
guess = random.randint(x, y)
answer = input("Is your number ", guess, "? Or is it 'lo'wer or 'hi'gher?")
if answer == "hi":
x = guess + 1
if answer == "lo":
y = guess - 1
tries += 1
print ("Got it! Your number is ", los, "! It took me ", tries, " Tries! :)")
input("End")
但是我收到一条错误消息:
Traceback (most recent call last):
File "/home/Documents/python/numbers.py", line 11, in <module>
answer = input("Is your number ", guess, "? Or is it 'lo'wer or 'hi'gher?")
TypeError: input expected at most 1 arguments, got 3
我知道我不应该期望
input
接受超过 1 个参数,但是我该如何解决这个问题?
您可以在输入上方添加一行并稍微更改输入函数调用。
import random
print("Pick any number between 1-100 and I'll try to guess it")
x = 1
y = 100
tries = 0
answer = "whatever"
while answer != "yes":
guess = random.randint(x, y)
input_message = "Is your number ", guess, "? Or is it 'lo'wer or 'hi'gher?"
answer = input(input_message)
if answer == "hi":
x = guess + 1
if answer == "lo":
y = guess - 1
tries += 1
print ("Got it! Your number is ", los, "! It took me ", tries, " Tries! :)")
input("End")