[Tutor] Coding

Alan Gauld alan.gauld at btinternet.com
Tue Dec 9 11:58:10 CET 2014


On 08/12/14 23:06, Awais Idris wrote:
> The problem is when i enter my first choice it accepts it fine and well.
> When it says would you like a second item, andi say yes it listd the
> options and says have a good day and the program cuts off. In wondering if
> this is a problem with the statements?

As Danny pointed out both Steven and I suggested a better way to test. 
In case you missed it here is Steven's response:

################################
Actually it sees it as:

if ((FirstChoice == 'Coke') or 'Pepsi' or 'Water'):

which will always evaluate as True.

*Technically* it will evaluate as either True or 'Pepsi', which is a
truthy value, so the if block will always run.

What we actually want is one of these:

# The long way
if (FirstChoice == 'Coke') or (FirstChoice == 'Pepsi') or (FirstChoice 
== 'Water'):


# The short way
if FirstChoice in ('Coke', 'Pepsi', 'Water'):

###################################

And since you already have the drinks in a set you can
shorten the last line even further to:

if FirstChoice in drinks:



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list