繁体   English   中英

如何让程序仅在特定情况发生时停止?

[英]How do I make the program stop only when something specific happens?

我刚开始编码,我想写一个小程序,你必须猜一个数字。 问题是你基本上只有一个猜测,然后它告诉你正确的数字。 如何让程序仅在用户猜对数字后立即停止?

这是我当前的代码:

import random

a = int(input())

x = random.randint(1,5)

if a > x:
    print("you guessed to high")

elif a < x:
    print("you guessed to low")

elif a == x:
    print("you guessed right")

我试过'while'但没有用。

我想你能弄清楚发生了什么

import random

x = random.randint(1,5)

guessed_right = False

while not guessed_right:

    a = int(input())

    if a > x:
        print("you guessed to high")

    elif a < x:
        print("you guessed to low")

    elif a == x:
        print("you guessed right")
        guessed_right = True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM