简体   繁体   中英

Python won't break from a while True

this is my code but as the title says I can not break and if I put something outside the while True and lives go below 0 nothing happens.

from random import randint
    import sys
    value = int(randint(1,10))
    lives = int(5)
    userinp = int(0)
    if lives == 0:
        print("no lives :(")
    while True:
        if lives != 0:
            print(value)
            print("You currently have: ", lives, " lives left")
            userinp = input("Please enter a number from 1 to 10: ")
            if int(userinp) == int(value):
                print("Well done you wn :D")
                sys.exit()
            elif int(userinp) > int(value):
                print("Your guess was too high please guess again")
                lives = lives - 1
            elif int(userinp) < int(value):
                print("Your guess was too low please guess again")
                lives = lives - 1
    if lives == 0:
        print("this")

Your loop essentially looks like:

while True:
    if lives != 0:
        # do stuff with lives

This is an infinite loop. The loop wont break even if lives == 0 , because this condition is tested as part of the loop block, not as the loop condition. You should simply do :

while lives != 0:
    # do stuff with lives

Or even while lives > 0: in case you manage to loose several lives during 1 loop iteration (doesn't seem to be possible here but better safe than sorry).

And since you seem to want to learn Python, there are a few other things you may want to learn and improve also:

No need to convert integers to integers here:

value = int(randint(1,10))
lives = int(5)
userinp = int(0)

Numbers like 5 and 0 , and randint(1,10) are integers by themselves. "5" and "0" are strings containing a number as single character and would need to be converted before being compared to integers, but it is not needed here. randint() returns integers.

This is dead code since you have set lives to 5 2 lines above and not modified it afterwards:

if lives == 0:
    print("no lives :(")

You should check if the user actually inputs a valid integer convertible value instead of an arbitrary string: ( continue will skip to the next loop iteration, so it will ask the user for input again without decrementing lives

userinp = input("Please enter a number from 1 to 10: ")
if not userinp.isnumeric(): continue

No need to convert your already integer value to an integer again and again and AGAIN:

if int(userinp) == int(value):

elif int(userinp) > int(value):

elif int(userinp) < int(value):

Avoid using sys.exit() , you may just break instead (to get out of the loop and continue.

And finally, Python has handy -=, +=, *=, /= operators, so you can write lives -= 1 instead of lives = lives - 1

A while True is an infinite loop, consuming 100% of all CPU cycles and will never break - since a while <condition> only ends when the condition becomes False , but that can never be, because it is always True !

Try not fighting the nature of your loop:

while True:
    if lives != 0:
        print(value)
        print("You currently have: ", lives, " lives left")
        userinp = input("Please enter a number from 1 to 10: ")
        if int(userinp) == int(value):
            print("Well done you wn :D")
            sys.exit()
        elif int(userinp) > int(value):
            print("Your guess was too high please guess again")
            lives = lives - 1
        elif int(userinp) < int(value):
            print("Your guess was too low please guess again")
            lives = lives - 1
    if lives == 0:  # indent condition to fall within the loop
        print("this")
    if lives < 0:  # add an extra case to handle what you want
        break

你必须打破whilelives == 0 ,或者由break或改变时的状态。

This is my solution, you can set the break condition specifically to end your while loop if a specific event occurs.

while lives > 0:
    print(value)
    print("You currently have: ", lives, " lives left")
    userinp = input("Please enter a number from 1 to 10: ")
    if int(userinp) == int(value):
        print("Well done you wn :D")
        sys.exit()
    elif int(userinp) > int(value):
        print("Your guess was too high please guess again")
        lives = lives - 1
    elif int(userinp) < int(value):
        print("Your guess was too low please guess again")
        lives = lives - 1


print("this")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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