如何让这段代码可以重复,而不是每次结束后需再次运行,求教各位大神

number = 59
guess = int(input('Enter an integer:'))
# print("guess is:" + str(guess))
if guess == number:
    #New block starts here
    print('Bingo!you guess it right.')
    print('(but you do not win any prizes!)')
    #New block ends here
elif guess  < number:
    #Another block
    print('No,the number is higher than that')
    #You can do whatever you want in a black...
else:
    print('No,the number is a lower than that')
    #You must have guessed > number to reach here
print('Done')

number = 59
while True: 
    guess = int(input('Enter an integer:'))
    # print("guess is:" + str(guess))
    if guess == number:
        #New block starts here
        print('Bingo!you guess it right.')
        print('(but you do not win any prizes!)')
        #New block ends here
        break
    elif guess  < number:
        #Another block
        print('No,the number is higher than that')
        #You can do whatever you want in a black...
    else:
        print('No,the number is a lower than that')
        #You must have guessed > number to reach here
print('Done')


# Output
Enter an integer:23
No,the number is higher than that
Enter an integer:80
No,the number is a lower than that
Enter an integer:59
Bingo!you guess it right.
(but you do not win any prizes!)
Done

 

while(True):
    number = 59
    guess = int(input('Enter an integer:'))
    # print("guess is:" + str(guess))
    if guess == number:
        #New block starts here
        print('Bingo!you guess it right.')
        print('(but you do not win any prizes!)')
        #New block ends here
    elif guess  < number:
        #Another block
        print('No,the number is higher than that')
        #You can do whatever you want in a black...
    else:
        print('No,the number is a lower than that')
        #You must have guessed > number to reach here
    print('Done')

用while语法 把代码包起来。

while(1){
number = 59
guess = int(input('Enter an integer:'))
# print("guess is:" + str(guess))
if guess == number:
    #New block starts here
    print('Bingo!you guess it right.')
    print('(but you do not win any prizes!)')
    #New block ends here
elif guess  < number:
    #Another block
    print('No,the number is higher than that')
    #You can do whatever you want in a black...
else:
    print('No,the number is a lower than that')
    #You must have guessed > number to reach here
print('Done')
}

在while中加条件判断,设置为1一直循环,想停止可以判断某个变量的变化情况来控制

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y