Shampoo Cycle Loop

Shampoo Cycle Loop

I am having trouble figuring out how to stop the loop in my code:

def shampoo_instructions(num_cycles):
    for num_cycles in range(1,num_cycles+1):
        if num_cycles < 1:
            print 'Too few.'
        elif num_cycles > 4:
            print 'Too many.' 
        else:
            print num_cycles, ': Lather and rinse.'
    else:     
        print 'Done.'

shampoo_instructions(2)

My output would be:

1 : Lather and rinse.

2 : Lather and rinse.

Done.

How can i make it so when shampoo_instructions(6)

it just prints "Too many."

?

2

2 Answers

Move your range check to be outside your actual looping, eg:

def shampoo_instructions(num_cycles):
    if num_cycles < 1:
        print 'Too few.'
    elif num_cyles > 4:
        print 'Too many.'
    else:
        for num_cycles in range(1,num_cycles+1):
            print num_cycles, 'lather and rinse.'
        print 'Done'
3

found some help from reddit.

def shampoo_instructions(num_cycles):
    if num_cycles <= 0:
        print("Too few.")
    elif num_cycles >= 5:
        print("Too many.")
    else:
        for num_cycles in range (1, num_cycles + 1):
            print(num_cycles,":", "Lather and rinse.")
        print("Done.")
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.