首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >if语句循环和while循环

if语句循环和while循环
EN

Stack Overflow用户
提问于 2021-04-26 04:15:34
回答 1查看 44关注 0票数 0
代码语言:javascript
运行
复制
number = int(input("please choose your number: ")) 

while number > number_to_guess: 
    number = int(input("Your guess is wrong it was bigger then the generated number, try again: "))
while number < number_to_guess : 
     number = int(input("Your guess was wrong it was smaller then the generated number, try again: "))

if number == number_to_guess:
    print("Congrats you won")
    restart = input("Do you want to play again? if yes type y, if not you can close the window \n")

我试图创建一个循环,并给出用户必须猜测的数字的线索,我第一次尝试if语句,当然它没有循环,我尝试了多种方法这是我能想到的最好的方法,但它并不完全有效,它会一直告诉我,例如:它变小了,但当它变大时,它就停止了,程序没有发送任何东西,但如果我得到了正确的数字,它会说恭喜,我也想让它在用户获胜并输入y后重新开始,但我完全不知道如何做到这一点

EN

Stack Overflow用户

回答已采纳

发布于 2021-04-26 04:22:54

请按此顺序尝试

代码语言:javascript
运行
复制
number = int(input("please choose your number: "))
number_to_guess = 5
while number != number_to_guess:
    if number > number_to_guess:
        number = int(input("Your guess is wrong it was bigger then the generated number, try again: "))
        continue
    if number < number_to_guess :
        number = int(input("Your guess was wrong it was smaller then the generated number, try again: "))
        continue
print("Congrats you won")
restart = input("Do you want to play again? if yes type y, if not you can close the window \n")
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67257862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档