On Tue, 06 Aug 2013 22:35:44 +0100, wrote:
Why won't the 'goodbye' part of this code work right? it prints 'ok' no
matter what is typed. Much thanks.
if goagain=='y' or 'yes':
This line doesn't do what you think it does :-) Typing that condition at
an interactive prompt reveals something interesting:
goagain = "n"
Oho. What's actually happening is that you've mistaken the operator
precedence. "==" has a higher precedence than "or", so your condition is
equivalent to '(goagain=="y") or "yes"'. Since it's left-hand argument is
False, "or" returns its right-hand argument, which has the value 'yes',
which in a boolean context is "True".
What you seem to want to do is to have your condition be true if goagain
is either "y" or "yes". Probably the easiest way of doing this and
learning something new at the same time is to ask if goagain appears in a
list (or rather a tuple) of strings:
if goagain in ('y', 'yes'):
elif goagain not in ('y', 'yes'):
if goagain in ('y', 'yes', 'ohdeargodyes', 'you get the idea'):
Rhodri James *-* Wildebeest Herder to the Masses