问题描述
print("Guess the hidden number between 1 and 100")
guess = int(input("Enter your guess:\n")
if guess==67
print("Correct Well Done")
elif guess<67
print("Your guess is too low. Try again.")
else guess>67
print("Your guess is too high. Try again.")
然后我希望它在每次用户输入答案时重复此操作,然后当他们最终做对时,它就会停止。
1楼
可以将一个简单的 while 循环添加到您的代码中。 但是请注意,对猜测值进行硬编码不会产生最易于维护的代码。
guess = 0
while guess != 67:
print("Guess the hidden number between 1 and 100")
guess = int(input("Enter your guess:\n")
if guess==67:
print("Correct Well Done")
elif guess<67:
print("Your guess is too low. Try again.")
else guess>67:
print("Your guess is too high. Try again.")
您可能想要实现一个“找到”变量并在找到时跳出循环
found = false
while found == False:
print("Guess the hidden number between 1 and 100")
guess = int(input("Enter your guess:\n")
if guess==67:
print("Correct Well Done")
found = True
elif guess<67:
print("Your guess is too low. Try again.")
else guess>67:
print("Your guess is too high. Try again.")
2楼
尝试这个。
添加了冒号“:”。
您的代码不起作用的原因是if
和elif
之后缺少冒号。
print("Guess the hidden number between 1 and 100")
guess = int(input("Enter your guess:\n")
if guess==67:
print("Correct Well Done")
elif guess<67:
print("Your guess is too low. Try again.")
else guess>67:
print("Your guess is too high. Try again.")