Python Forum
Help needed to troubleshoot.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed to troubleshoot.
#1
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")
Reply
#2
Check this out.
Reply
#3
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"]:
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  strategy to troubleshoot what pyinstaller misses hammer 0 946 May-23-2022, 01:05 AM
Last Post: hammer

Forum Jump:

User Panel Messages

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