6.1k views
4 votes
Write a function shampoo_instructions() with parameter num_cycles. If num_cycles is less than 1, print "Too few.". If more than 4, print "Too many.". Else, print "N : Lather and rinse." num_cycles times, where N is the cycle number, followed by "Done.".

1 Answer

9 votes

Answer:

Following are the code to the given question:

def shampoo_instructions(num_cycles):#defining a method shampoo_instructions that accepts num_cycles as a parameter

if num_cycles < 1:#defining an if block that num_cycles value is less than 1

print('Too few.')#print message

elif num_cycles > 4:#defining elif block that checks num_cycles is greater than 4

print('Too many.')#print message

else:#defining else block

N= 1;#defining N variable that holds a value 1

for N in range(N, num_cycles+1):#defining for loop that check value in range

print(N , ": Lather and rinse.")#print message

print('Done.')#print message

shampoo_instructions(2)#calling the method

Output:

1 : Lather and rinse.

2 : Lather and rinse.

Done.

Explanation:

In this code, a method "shampoo_instructions" is defined, that accepts a variable "num_cycles" in its parameter, and in the next step, the multiple conditional statements have used that check the "num_cycles" values which can be defined as follows:

  • The if the block checks the parameter value is less than 1, and it prints the value as the message.
  • In the next step, the elif block is used that checks parameter value is greater than 4, and it prints the value as the message.
  • In the else block, it uses the for loop that checks the value in the range and prints its value and calling the method.
User Zxz
by
5.5k points