简体   繁体   中英

While loop not breaking(python)

Here based on the conditions value of cows is set. And if cows is equal to 4 then the while loop should break. But here break is treated as if it is not present.

import random

r = random.randint
def get_num():
 return "{0}{1}{2}{3}".format(r(1, 9), r(1, 9), r(1, 9), r(1, 9))

n = get_num()
print(n)
n = [z for z in str(n)]

def game():
    cows = 0
    bulls = 0
    print()

    usr_num = [i for i in input("enter:\n")]
    usr_set = set(usr_num)

    while True:
        for x in usr_set:
            if usr_num.count(x) >= n.count(x):
                cows += n.count(x)
                bulls += usr_num.count(x) - n.count(x)
            elif usr_num.count(x) < n.count(x):
                cows += usr_num.count(x)
                bulls += n.count(x) - usr_num.count(x)

        print("cows: ", cows, "   bulls: ", bulls)

        if cows == 4:
            print("correct!")
            break
        else:
            game()

game()

When cows = 4 , correct is printed but break is not showing its effect

And if we slightly change the code. In place of cows if we put 4 (If statement)

def game():
    cows = 0
    bulls = 0
    print()

    usr_num = [i for i in input("enter:\n")]
    usr_set = set(usr_num)

    while True:
        for x in usr_set:
            if usr_num.count(x) >= n.count(x):
                cows += n.count(x)
                bulls += usr_num.count(x) - n.count(x)
            elif usr_num.count(x) < n.count(x):
                cows += usr_num.count(x)
                bulls += n.count(x) - usr_num.count(x)

        print("cows: ", cows, "   bulls: ", bulls)

        if 4 == 4:
            print("correct!")
            break
        else:
            game()

game()

Then break is working.

You are recursing each time you do another round, which means when you break , you only end up breaking out of the last recursion.

Instead of using tail recursion, try moving the while True:

def game():
    while True:
        cows = 0
        bulls = 0
        print()

        usr_num = [i for i in input("enter:\n")]
        usr_set = set(usr_num)

        for x in usr_set:
            if usr_num.count(x) >= n.count(x):
                cows += n.count(x)
                bulls += usr_num.count(x) - n.count(x)
            elif usr_num.count(x) < n.count(x):
                cows += usr_num.count(x)
                bulls += n.count(x) - usr_num.count(x)

        print("cows: ", cows, "   bulls: ", bulls)

        if cows == 4:
            print("correct!")
            break

This way we don't recurse, so our break works as you expect: see repl.it

I just tried running your code and here are more problems with your script than just the while loop.

But try this little script to learn how a while loop works:

# While loop test

i=0
j=5
while True:
    if i >= j:
        break
    else:
        print(f"{i} < {j}")
        i +=1

Hope this helps. have a play.

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