[Tutor] Help with else

Alan Gauld alan.gauld at btinternet.com
Fri Oct 9 11:21:17 CEST 2015


On 09/10/15 04:31, Nick Brodsky wrote:
> name = raw_input("What is your name")
>
> print "Great! Now %s, are you a boy or a girl?" % (name)
>
> gender = raw_input("")
>
> if gender == "boy":
>    print " I can see that, you are very strong":
> if gender == "girl":
>    print ' Yes, and a beautiful one':
> else:
>    print "Your choice"
>
>
> I don't understand why it doesn't work, it just skips over the else.

Not for me it didn't, it gave an error.

Please always post exactly what the program produces
don't try to summarize.

In this case there are several potential errors but the first
thing to fix are the colons after the print statements. You
only need colons on the end of control statements such as
for, while, if/else etc.

The second thing that I see is that although (I assume)
you only want the 'your choice' to print if the user
selects something other than boy/girl it will print
even for boy. That's because the boy section is in
a separate if statement.

Like this simplified snippet:

if boy: print....

if girl: print...
else: print...


But I think you wanted

if boy: print...
elif girl: print ...
else: print...

Can you see the difference in logic?

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