Home > OS >  Infinite Loop help in Python
Infinite Loop help in Python

Time:09-28

Can someone help me figure out why this loop is infinite? The class I am in automatically inputs the variables for me per those last 2 lines. It passes the test with numbers 2 and 4. However there is another input, that I am unable to see, that keeps this running as an infinite loop. I can not figure out where there is a gap in this code that would allow an infinite loop. Any suggestions?

def shampoo_instructions(user_cycles):
    N = 1
    while N <= user_cycles:
        if N < 1:
            print('Too few')
        elif N > 4:
            print('Too many')
        else:
            print(N,': Lather and rinse.')
            N = N   1
    print('Done.')
                
user_cycles = int(input())
shampoo_instructions(user_cycles)

CodePudding user response:

Indent N = N 1 out of the loop, otherwise it never get's to adding.

Or better use N = 1:

def shampoo_instructions(user_cycles):
    N = 1
    while N <= user_cycles:
        if N < 1:
            print('Too few')
        elif N > 4:
            print('Too many')
        else:
            print(N,': Lather and rinse.')
        N = N   1
    print('Done.')
                
user_cycles = int(input())
shampoo_instructions(user_cycles)

CodePudding user response:

Your indentation at line N = N 1 would need to have the same level as the if-else statement.

Therefore, it would be like:

def shampoo_instructions(user_cycles):
    N = 1
    while N <= user_cycles:
        if N < 1:
            print('Too few')
        elif N > 4:
            print('Too many')
        else:
            print(N,': Lather and rinse.')
        N = N   1
    print('Done.')
                
user_cycles = int(input())
shampoo_instructions(user_cycles)

Thanks for your question.

CodePudding user response:

First: get used to testing your code. Since you have conditionals involving the numbers 1 and 4, you should test numbers that are less than 1 and greater than 4 to see what happens beyond those edges. Sure enough, giving 5 as an input produces an infinite loop:

0
Done.
1
1 : Lather and rinse.
Done.
4
1 : Lather and rinse.
2 : Lather and rinse.
3 : Lather and rinse.
4 : Lather and rinse.
Done.
5
1 : Lather and rinse.
2 : Lather and rinse.
3 : Lather and rinse.
4 : Lather and rinse.
Too many
Too many
Too many
Too many
Too many
Too many

Why does that happen? user_cycles == 5 so the loop won't stop until N == 6 (or any value greater than 5.

What happens when N == 5? We print "Too many" and then continue the loop without incrementing N again. Hence the loop will always get stuck at N = 5.

Note that testing with these values also shows is that we never hit the Too few condition -- this is dead code! It's not possible to ever reach this condition because N always starts at 1 and is never decreased.

The way to fix the infinite loop depends on the desired behavior. You could break the loop as soon as N exceeds 4:

        elif N > 4:
            print('Too many')
            break

Another option would be to make sure that N is always incremented, either by incrementing it inside that conditional block or incrementing it outside the entire if...elif...else statement rather than inside the else (which will only run for values between 1 and 4).

  • Related