当前位置: 代码迷 >> python >> Python 循环和输出不起作用
  详细解决方案

Python 循环和输出不起作用

热度:104   发布时间:2023-06-16 14:06:13.0
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.")

然后我希望它在每次用户输入答案时重复此操作,然后当他们最终做对时,它就会停止。

可以将一个简单的 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.")

尝试这个。 添加了冒号“:”。 您的代码不起作用的原因是ifelif之后缺少冒号。

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.")
  相关解决方案