Python Forum
Potential confusion combining != with or
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Potential confusion combining != with or
#5
(Oct-29-2019, 08:02 PM)Mark17 Wrote:
answer.lower() != ("y" or "n"):

No-one has explained why this is wrong. In Python (and other languages too, actually), all values can be tested for being True or False (see the docs). Non-empty strings are treated as True, so the expression "y" or "n" evaluates to True. You then compare the return value of answer.lower() with True and the expression answer.lower() != True will always evaluate to True because answer.lower() returns a string and values of different types (string and bool here) will never be equal.
Reply


Messages In This Thread
RE: Potential confusion combining != with or - by ndc85430 - Oct-31-2019, 06:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to use the full potential of scalene in Pycharm? arnoldpredator 4 855 Nov-18-2023, 01:46 PM
Last Post: arnoldpredator
  Potential Permission error on Mac OSX Catalina OWOLLC 1 932 Nov-02-2023, 07:52 AM
Last Post: unjnsacih
  Potentiostat/Galvanostat potential issue pepe523869 10 2,826 Nov-05-2022, 07:08 PM
Last Post: pepe523869

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020