If and/or Else not working!

Question:
<Please help!>
Repl link:
https://replit.com/@TrijalSaha1/WuT-Is-ThE-DaTe-AnD-TiMe

code snippet

I mean this is the link: https://replit.com/@TrijalSaha1/WuT-Is-ThE-DaTe-AnD-TiMe?v=1

Just reply already WindLother

When you say if AskTimeAndDate == "Y" or "y" or "Yes" or "yes": what you’re effectively saying is:

if (AskTimeAndDate == "Y") or "y" or "Yes" or "yes":

You see the problem?

The correct way to structure this is to make sure you’re comparing AskTimeAndDate to each string you’re interested in. So, the if should be:

if AskTimeAndDate == "Y" or AskTimeAndDate == "y" or AskTimeAndDate == "Yes" or AskTimeAndDate == "yes":

Or being more efficient, just use:

if AskTimeAndDate.lower() in ["y", "yes"]:

The lower() function makes the input lowercase so that it doesn’t matter if the user enters ‘Y’, ‘y’, ‘Yes’, or ‘yes’. It will work for all of them.

3 Likes

THANKS! Youre the best!

I always give a full response of what the person is doing wrong and what they should be doing, so my answers tend to take some time.

2 Likes

AskTimeAndDate is not a conventional identifier (variable name) that you’d use here. UpperCamelCase is used for Classes, including Exceptions, while variables conventionally use snake_case, e.g. here I would use want_datetime. Alternatively, camelCase is used often in JS, and that doesn’t cause any confusion with classes unlike UpperCamelCase but it isn’t conventional in Python and Rust.


here, the difference is unnoticeable, but it’s more performant to compare against sets, so I would do

if want_datetime.lower() in {"y", "yes"}:
  ...
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.