Beginner question

E

eschneider92

Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks.

def thing():
print('go again?')
goagain=input()
if goagain=='y' or 'yes':
print('ok')
elif goagain!='y' or 'yes':
print('goodbye')
sys.exit()
thing()
 
C

Chris Down

Why won't the 'goodbye' part of this code work right? it prints 'ok' no
matter what is typed. Much thanks.

"if" statements do not fall through, because the first statement was matched,
no other ones in the same chain will be evaluted.

"elif" means "else if", where "else" means "if nothing previous matched".

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQEcBAEBAgAGBQJSAW7LAAoJEK7pd0+nzJtNhLUIAIN0feaYYURSB2WuJ3u7aF+J
Zb1a84jhRCu28CrR5y+exnydriU0JVPZhGKJP7RDJVJG12uO8lXPCsd7wTHtjCMn
5GSz3+1EgGaMWqotkfMT8TVZJORssqHsyEsAOY4LF/3r4CCZu1EKIvsDvIpg06Kv
4g5kmhKI0GZPyLi2KLqkymG34PUrWeK+bHA+na1fYNsGcnoTgJd2vjG3Tn2iuNHV
M2A4YPdNmxe3/7SqRjo8CCYkD7OtESoFFTDRgtaSWReco/5Rv+yNjG56RrAoCygs
8NPZViiGiGzizanLno7GrEJKFw5/81yPaP1e+HgofwCqKfe4/4wVxHnIgSS392A=
=bLnf
-----END PGP SIGNATURE-----
 
D

Dave Angel

Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks.

def thing():
print('go again?')
goagain=input()
if goagain=='y' or 'yes':

This expression doesn't do what you think. The comparison binds more
tightly, so it first evaluates (goagain=="y"). The results of that are
either True or False. Then it or's that logical value with 'yes'. The
result is either True or it's 'yes'. 'yes' is considered truthy, so
the if will always succeed.

What you meant to use was:
if goagain == "y" or goagain == "yes":

Alternatively, you could use
if goagain in ("y", "yes"):
print('ok')
elif goagain!='y' or 'yes':

Same here.
 
C

Chris Angelico

Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks.

def thing():
print('go again?')
goagain=input()
if goagain=='y' or 'yes':
print('ok')
elif goagain!='y' or 'yes':
print('goodbye')
sys.exit()
thing()

When you use 'or' in this way, it's not doing what you think it does.
It actually first computes

which is either True or False, and then evaluates the next part:

Try out those two in interactive Python and see what they do. You
probably want to compare in both halves of the test, or possibly use
the 'in' operator:

if goagain in ('y', 'yes'):

Also, you don't need an 'elif' there; just use a plain else. Repeating
the condition just introduces the chance of bugs - for instance, would
you notice the error in this?

if goagain=='y' or goagain=='yes':
print('ok')
elif goagain!='y' or goagain!='yes':
print('goodbye')

Using a plain 'else' guarantees that exactly one of the branches will
be executed, rather than having the possibility that neither will,
which isn't the intention here.

Hope that helps!

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top