-2
if AMPM is not "AM" or AMPM is not "PM":
    AMPM = input('You entered an incorrect value for AMPM. Please, try again: ')

It just completely ignores the "Is not" part. I've tried doing != and I get the same result, where it just continues the if statement. Am I doing something wrong?

the complete code: https://pastebin.com/t6WmqPTr

Gregory
  • 1
  • 5
  • 1
    `!=` is what you want. If that's not working, your example isn't representative. – erip May 21 '17 at 17:48
  • 4
    If they enter `AM`, that's not `PM`, and if they enter `PM`, that's not `AM`. – user2357112 May 21 '17 at 17:48
  • Ah I see. Thank you – Gregory May 21 '17 at 17:50
  • Possible duplicate: [http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce](http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce) – mx0 May 21 '17 at 17:50
  • 2
    The statement will always evaluate to `False`. As @user2357112 mentioned, one of the two statements will always evaluate to `False`, hence the whole statement will as well. – Brad Solomon May 21 '17 at 17:51

4 Answers4

3

The expression is incorrect outright, but I would recommend a more idiomatic approach anyway:

if AMPM not in ('AM', 'PM'):
    # statement

This way, you're guaranteed that you'll get the right result if AMPM happens to be set to "foo".

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Because AMPM cannot be both "AM" and "PM", the input statement will always be run. You should use and instead of or. Also, you should use != rather than is not. Although is not will work in some cases due to string interning.

Davis Yoshida
  • 1,757
  • 1
  • 10
  • 24
0

I fixed it! Rundown:

It saw that AMPM was am, so thats ok. Then it saw AMPM wasn't PM, so it continued. This is my solution:

if AMPM != "AM":
  if AMPM != "PM":
    AMPM = input('You entered an incorrect value for AMPM. Please, try again: ')
Gregory
  • 1
  • 5
0

Looking at your full code, you have the following:

if AMPM == "Am" or AMPM == "am":
    AMPM = "AM"
if AMPM == "Pm" or AMPM == "pm":
    AMPM = "PM"

if AMPM is not "AM" or AMPM is not "PM":
    AMPM = input('You entered an incorrect value for AMPM. Please, try again: ')

You already checked everything you need above! The part you asked about is what you want to perform when all else fails. This is what the else clause is for. So you can fix the code (and make it more clean and readable) like this:

if AMPM == "Am" or AMPM == "am":
    AMPM = "AM"

elif AMPM == "Pm" or AMPM == "pm":
    AMPM = "PM"

else:
    AMPM = input('You entered an incorrect value for AMPM. Please, try again: ') 

In case this is new - elif means 'else-if'. If the first if is evaluated to false, the program checks the next elif after it. If all clauses are false, the else section is executed.

alonav11
  • 1
  • 1