-1

Feel like this should simply work but its not. Works when its simple if != but not when using or. I know I can do it the other way but this should work...Here is a sample

today = input('Enter Day of the week (Sun, Mon, Tue and etc) ')
if today != 'Sun' or today != 'Sat':
     print ('Go to work!')
else:
   print('Weekend!!')
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 4
    Your logic is flawed, it can't be both of those strings simultaneously so either of these comparisons is always true. true OR'ed with anything is true. – tkausl Sep 19 '21 at 19:48
  • Is `'Sun' != 'Sat'` true? Is `False or 'Sun' != 'Sat'` true? – Joseph Sible-Reinstate Monica Sep 19 '21 at 19:49
  • Did you write print(today) at the end of the string? – Ostriy Пончик Sep 19 '21 at 19:51
  • 1
    You need to study de Morgan’s law. – quamrana Sep 19 '21 at 19:55
  • Thanks for your quick reply but lets say 'Mon' != 'Sat' or 'Mon' != 'Sun' should go to else but it doesn't thanks – Peter Barash Sep 19 '21 at 19:58
  • @PeterBarash no. It shouldn't go to else. `'Mon' != 'Sat'` is true. then, that implies`('Mon' != 'Sat') or WHATEVER` is also true, since `True or False` is `True` and `True or True` is `True` – juanpa.arrivillaga Sep 19 '21 at 20:23
  • @juanpa.arrivillaga thanks makes sense and it is bad example what if I need to compare two different variables and if one not equal or other not equal For example major=input() year=input() if major!='Math' or year!='Junior' print('Must take Lit class') I need to print message only if major!='Math' or year!='Junior' Thanks – Peter Barash Sep 19 '21 at 20:42

1 Answers1

2

It needs to be:

if today != 'Sun' and today != 'Sat':
     print ('Go to work!')

Because it is always either not Sunday OR not Saturday. Even on Sunday it is not Saturday. So, your statement will always be true. But if it is both not Sunday AND not Saturday, then go to work.

Martin Wettstein
  • 2,771
  • 2
  • 9
  • 15
  • Thank you! Maybe bad example what if I need to compare two different variables and if one not equal or other not equal For example major=input() year=input() if major!='Math' or year!='Junior' print('Must take Lit class') – Peter Barash Sep 19 '21 at 20:05
  • 1
    The same applies there. If you want both not to be true at the same time, use `and`. If it's enough that one is not true, use `or`. And as soon as you have written the condition, assume values and check whether it would result in `True` or `False`. Do this by hand on a slip of paper and do it for many different values that should result in a true or false output. – Martin Wettstein Sep 20 '21 at 06:15