1

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)
5
  • 1
    Indentation is all messed up. As far as I can tell (if I make some guesses at how to fix the indentation) this function will just exit immediately if you give it anything other than 1, which makes me think this isn't the code you're actually running. Commented Sep 28, 2021 at 1:40
  • Welcome to SO. Please fix your indentation. Commented Sep 28, 2021 at 1:41
  • I have added with updated indentation. Sorry. @ewong Commented Sep 28, 2021 at 1:44
  • I have added with updated indentation. Sorry. @Samwise Commented Sep 28, 2021 at 1:45
  • In your own words, what has to be true in order for N = N + 1 to happen? In your own words, if that doesn't happen, then why should the result of N <= user_cycles change? If that doesn't happen, why would the loop end? Commented Sep 28, 2021 at 2:34

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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).

1 Comment

Thank you so much for the detailed explanation! That is worded exactly how I needed it to be for ease of understanding. Your help is much appreciated! This helped me to see that I had also not set limits for the variable user_cycles, which is where the input is generated. Again, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.