For Loop Not Breaking (Python)

后端 未结 8 663
春和景丽
春和景丽 2021-01-22 11:35

I\'m writing a simple For loop in Python. Is there a way to break the loop without using the \'break\' command. I would think that by setting count = 10 that the exit condition

8条回答
  •  温柔的废话
    2021-01-22 12:24

    You can use while:

    times = 5
    guessed = False
    while times and not guessed:
        guess_number = int(input("Enter any number between 0 - 10: "))
        if guess_number == rand_number:
            print("You guessed it!")
            guessed = True
        else:
            print("Try again...")
                times -= 1
    

提交回复
热议问题