Python Forum

Full Version: Help needed to troubleshoot.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello I not very good with python i still learn. I use Python 3 on trinket (website) because our school we only have chromebooks. I have had this issue many times and i do not know how to fix. basicly if the input is yes, Yes, Y or y it should print yes and if the input is No, no, N or n it should print no, but it alway print yes. i dont know to fix. Here is code.

print("Woldpack TDC Assist \n") print("Range To Target") mast_height=float(input("Mast height")) lines_amount=float(input("Lines amount")) zoom=input("Are you zoomed in?") if zoom=="Yes" or "yes" or "Y" or "y": print("yes") elif zoom=="No" or "no" or "N" or "n": print("No")
Check this out.
When you write

if zoom=="Yes" or "yes" or "Y" or "y":
Python parses the expression as (zoom=="Yes") or ("yes")... It's not comparing the bits after the or to the value in zoom, it's checking if they're true. Since "yes" evaluates to true (as does any non-empty string), then this conditional is always true.

You could instead do something verbose like:
if zoom == "Yes" or zoom == "yes" or zoom == "Y" .....
But you'd probably prefer:
if zoom in ["Yes", "yes", "Y", "y"]:
This is hilarious! I was just looking at some of the content over in the tutorials section and saw this:
if input_value == "Yes" or "yes" or "y" or "Y":
https://python-forum.io/Thread-Multiple-expressions-with-or-keyword

I thought "What a dumb example. Nobody would do that!" Guess I am the dumb one, forgetting not everyone has been programming for 40 years.