Jump to content


While Loop

  • Please log in to reply
1 reply to this topic

#1 Longhair


    New Member

  • Members
  • Pip
  • 4 posts

Posted 01 May 2014 - 06:33 AM

I am reviewing what I have read so far (upto page 83) and decided to apply it to the smoothie snippet.

01 def make_smoothie():
02     juice = input("What juice would you like? ")
03     fruit = input("OK - and how about the fruit? ")
04     print("Thanks. Let's go!")
05     print("Crushing the ice...")
06     print("Blending the " + fruit)
07     print("Now adding in the " + juice + " juice")
08     print("Finished! There's your " + fruit + " and " + juice + " smoothie!")
10 print("Welcome to smoothie-matic 2.0")
11 another = "Y"
12 while another == "Y":
13     make_smoothie()
14     another = input("How about another(Y/N)? ")
15 print("Please come again!")

When I type Y, the loop will start at line 02. No problems there.

When I type anything else N, n, y, ... , it goes to line 15.

I would like to add a loop to check to see if something other than Y or N has been entered.
  • If Y has been entered, goto line 02 (like it is now).
  • If N has been entered, have it go to line 15 (ending the program).
  • If anything else has been entered, have it print a message saying "Could you answer Y or N only?"

How would I accomplish the last part?

#2 #TM#


    Active Member

  • Members
  • PipPipPip
  • 68 posts
  • Gender:Not Telling

Posted 04 May 2014 - 03:32 AM

Good to know that you are applying your thought and imagination to programming.

Great ! Keep up !! :)

I think you will be able to understand from the code comments; so I am not writing any explanation.

def make_smoothie():
	juice = input("What juice would you like? ")
	fruit = input("OK - and how about the fruit? ")
	print("Thanks. Let's go!")
	print("Crushing the ice...")
	print("Blending the " + fruit)
	print("Now adding in the " + juice + " juice")
	print("Finished! There's your " + fruit + " and " + juice + " smoothie!")

print("Welcome to smoothie-matic 2.0")
flag = True  
# The trick is to check two conditions of variable 'flag', i.e., 'True' or 'False'
# You only want to get out of the loop when user say 'N',
# When user say 'Y' or user inputs arbitrary things you want to repeat the same msg.
# So, only run make_smoothie() function when user say 'Y'
while flag:
	# Bores down the user to hell until he/she enters Y or N
	another = input("How about another? ( Answer only as Y or N ) : ") 
	if another == "Y": # The flag is 'True'; but 'another == Y'? 
		make_smoothie() # so, run this
	elif another == "N": # user answer No
		flag = False # so flag = False. Process stops
		print("Please come again!") # while loop stops.

Still if you don't understand any line; feel free to discuss. :)

I am giving you a bit more to think and do.

user say "YES" or "Yes" or "Y" or "y" or etc.... he /she means the same, i.e., he/she has agreed to proceed.
Similarly for "NO" or etc...

So how will you handle all this situation ?
Try not to write all the type of "y"'s in your code.

hint: a built-in library method for strings; upper() or lower() might help.

Also look at the below link for a better solution.

The choice built-in python library

Edited by #TM#, 04 May 2014 - 04:33 AM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users