首页 > 解决方案 > 我的代码(一个 python 猜谜游戏)继续返回“你猜到了很高”,即使我输入了 9,即该范围内的最大值

问题描述

我认为问题是即使输入了正确的数字,循环也没有结束。但是,这条线(while choice != a :)应该可以防止这种情况发生,所以我不确定出了什么问题。

import random

a = random.randint(1, 9)
choice = int(input("give me a number"))

while choice != a :
    if choice > a:
        choice = int(input("You guessed too high. Please guess again"))
    elif choice < a:
        choice = int(input("You guessed to low. Please guess again"))

print("You won")

标签: python

解决方案


推荐阅读