【问题标题】:Python Loops and Outputs not workingPython循环和输出不起作用
【发布时间】:2015-10-25 20:24:30
【问题描述】:
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.")
然后我希望它在每次用户输入答案时重复此操作,然后当他们最终正确时,它会停止。
【问题讨论】:
标签:
python
loops
while-loop
【解决方案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.")