[Tutor] help!

Alan Gauld alan.gauld at btinternet.com
Sat Dec 19 19:33:20 EST 2015


On 19/12/15 17:39, Nathaniel Olivier via Tutor wrote:
> I don't understand what's wrong with this piece of code?

Thee are several problems

> print ("The Flying Circus recently entered your town and are doing a never
> seen before performance")
> answer=raw_input("Shall you go? Type Yes or No to answer.")
> 
> def the_flying_circus():
>     if answer == "Yes" or "yes" or "YES" or "yES":

THis is not doping what you think.
Python reads it like this:

if (answer == "Yes") or ("yes") or ("YES") or ("yES"):

And sinve the last three are all True in a boolean zsense the if is
always true.

You probably want to do two tyhings to fix this:

1) convert answer to lowercase (or upper if you prefer)
to avoid worrying about case combinations

if answer.lower() == 'yes':

2) use the in operator rather than or to cater for multiple
options, such as:

if answer.lower() in ('yes','y','yep','yup'):

>         print ("You decided to go but your wallet was stolen by a circus
> monkey. YOU LOSE!")
>     elif answer == "no" or "No" or "NO" or "nO":

same here

>         print ("Your friends pressure you into coming but you stay
> defiant.")
>         print ("
> 

Problem number 2.
Single quotes cannot bridge lines. You must use triple quotes (either
''' or """) to do that. OR you can embed newline literals in your single
quoted string:

print ("First line\n\n\n\n\n\\nlast line")

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