4
Join = input('Would you like to join me?')
if Join == 'yes' or 'Yes':
  print("Great," + myName + '!')
else:
  print ("Sorry for asking...")

So this is my code. It's longer; just including the problem. I'm asking a yes or no question and when in the console it runs smoothly until you get to it. Whatever you type you get the 'yes' output. Could someone please help? I've used elif statements as well but no luck.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
Ross Hudson
  • 83
  • 1
  • 2
  • 7
  • 2
    Did you mix up the indentation when you pasted the code, or is that how your code appears in your file? – 2rs2ts Jul 30 '13 at 17:52
  • @2rs2ts - He messed up the indentation when he pasted the code. He would have gotten a different error message otherwise. – Robᵩ Jul 30 '13 at 17:54
  • @Robᵩ Yeah, I know. I was trying to lead OP to the realization rather than saying "you pasted the code wrong." – 2rs2ts Jul 30 '13 at 17:55
  • 3
    Also these downvotes are ridiculous. "Issues with a if/else loop in Python" is a terrible description of the general problem here, and I doubt anything a beginner would think to search would link them to that question. – 2rs2ts Jul 30 '13 at 17:57

4 Answers4

16
if Join == 'yes' or 'Yes':

This is always true. Python reads it as:

if (Join == 'yes') or 'Yes':

The second half of the or, being a non-empty string, is always true, so the whole expression is always true because anything or true is true.

You can fix this by explicitly comparing Join to both values:

if Join == 'yes' or Join == 'Yes':

But in this particular case I would suggest the best way to write it is this:

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

This way the case of what the user enters does not matter, it is converted to lowercase and tested against a lowercase value. If you intend to use the variable Join elsewhere it may be convenient to lowercase it when it is input instead:

Join = input('Would you like to join me?').lower()
if Join == 'yes':   # etc.

You could also write it so that the user can enter anything that begins with y or indeed, just y:

Join = input('Would you like to join me?').lower()
if Join.startswith('y'):   # etc.
kindall
  • 178,883
  • 35
  • 278
  • 309
1

I answered this question yesterday

You can use .lower()

Join = input('Would you like to join me?')
if Join.lower() == 'yes':
  print("Great," + myName + '!')
else:
  print ("Sorry for asking...")
Community
  • 1
  • 1
Stephan
  • 16,509
  • 7
  • 35
  • 61
0

That's not how the or operator works. Replace:

if Join == 'yes' or 'Yes':

with:

if Join in ['yes', 'Yes']:

and it'll do what you want.

EDIT: Or try this, for more general purpose:

if 'yes'.startswith(Join.lower()):

which ought to match 'y', 'Y', 'ye', 'Ye', 'YE', and so on.

Crowman
  • 25,242
  • 5
  • 48
  • 56
  • That *is* how the `or` operator works. It's not how to use the `or` operator to achieve what OP intends, however. – 2rs2ts Jul 30 '13 at 17:54
  • Only if you choose to use 'that' to refer to something different to me - "That, being the desired behavior the OP wants from the `or` operator, is not how the `or` operator works." – Crowman Jul 30 '13 at 17:57
0

The if statement should actually be:

if Join=='yes' or Join =='Yes'

The way the if statement is written in your code will cause code to be evaluated this way:

(if Join == 'yes') or ('Yes'):

Note that ('Yes') is a truthy and will always evaluate to true

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22