[Tutor] invalid syntax (reply)
christopher.henk at allisontransmission.com
christopher.henk at allisontransmission.com
Fri Sep 25 00:14:07 CEST 2009
tutor-bounces+christopher.henk=allisontransmission.com at python.org wrote on
09/24/2009 05:43:24 PM:
> Robert, that is my code. The error? "Theres an error in your program :
> invalid syntax"
> Closing the parenthesis helped that one, but my else statement is not
> working...heres the code chunk...
> if wellness != "Good":
> elif wellness != "Well":
> elif wellness != "Fine":
> print "Oh, I'm sorry you are not feeling well. I guessed
> correct, right?"
> else print "Great!"
> candyNumber = raw_input("How many candies do you want? :")
> fatness = raw_input("How many candies do you want to eat? :")
> if fatness > candyNumber:
> print "HA! Nice try, but no. You don't have that many candies"
> else print "HA! You ate the candy! Suckerrrrr. You are now",
> fatness, "lbs overweight"
>
>
>
> my IDLE highlights the print, in each of them. Why? (oh, and thanks for
> helping, all)
I am not sure why Idle is higlighting your print statements ( I don't use
Idle, so will leave that to someone else).
However your if-elif-else construction should raise a syntax error. elif
means "else if" and should be matched up (at the same indentation level)
with an if statement just like an else statement. It is used when you
have more then two paths that the code can take depending on the test
results.
if test: (if test is true)
do something
elif another test: (else if another test is true)
do something else
else:
do a third thing
What you are trying to test is that wellness is not "Well", is not "good",
and is not "Fine"
If all of those is true (they are not good, not well and not fine) you
print your sorry message, otherwise you print great.
You have one fork in the path, and should have one if statement with one
else.
Hope that helps,
Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090924/e94297a9/attachment.htm>
More information about the Tutor
mailing list