首页 > 解决方案 > 我希望我的程序在 5x 试验结束时终止

问题描述

这是我的代码,一切似乎都正常,只有部分试用版不起作用。

counter = 1

max_attempt = 5
secrect_nu = 313
num = int(input("Guess my number: "))
while num != secrect_nu:
    max_attempt = max_attempt - 1
    print(emoji.emojize(":winking_face_with_tongue: :winking_face_with_tongue: \U0001F606"))
    num = int(input(f"HHHHH! You stuck in my loop you've {max_attempt} left \nGuess my number: "))
    if num >= max_attempt:
        print("Game Over!")
        break
    counter += 1

# while num >= max_attempt:
#
#     break

if num == secrect_nu:
    print("==========================================")
    print("Congrats, You've broken the chain of my loop.  ")
    print("You got my secrect number in your " + str(counter) + "th attempt.")

我想解决试用问题,如果用户超过 5 次试用,我希望程序停止

标签: python

解决方案


这样做应该对你有用:

counter = 1
max_attempt = 5
secrect_nu = 313
num = int(input("Guess my number: "))
while num != secrect_nu:
    max_attempt = max_attempt - 1
    num = int(input(f"HHHHH! You stuck in my loop you've {max_attempt} left \nGuess my number: "))
    if counter==4:
        print("Game Over!")
        break
    counter += 1

if num == secrect_nu:
    print("==========================================")
    print("Congrats, You've broken the chain of my loop.  ")
    print("You got my secrect number in your " + str(counter) + "th attempt.")

它将每次检查是否counter为 4 以及num是否等于secrect_nu


推荐阅读