Jump to content

<python> or operator- another stupid question

Another stupid question but this time its on the or operator.

 

import sys
num = (int(input("Enter the car number model or enter 0 to quit: ")))
if num == (221) or (780):
  print ("defective")
elif num == (123): 
  print ("working")
elif num == (0): 
  sys.exit()
else:
  print ("invaild number")
  

 

so when running this code for some reason it always replies with defective, i had done some trobleshooting and it turns out that just deleting the

 

or (780)

 

makes it work perfectly, now i can do another elif and just call it a day but what is wrong?

 

 

also can i make a thread with stupid questions about my problems cause making a new thread each time is kinda tiring...

|:Insert something funny:|

-----------------

*******

#

Link to comment
https://linustechtips.com/topic/1399820-or-operator-another-stupid-question/
Share on other sites

Link to post
Share on other sites

Hi! 

The problem is the the way you are reading it vs program reads it.

if num == (221) or (780):

You read this as if the number is equal to either of these two numbers

But, python reads this as

if number equals 221

or

if 780

When a number is by itself if it's not zero it's considered true, kind of a throwback to lower level languages even though it doesn't make a lot of sense on its face.

Link to post
Share on other sites

3 minutes ago, Logical92 said:

Hi! 

The problem is the the way you are reading it vs program reads it.

if num == (221) or (780):

You read this as if the number is equal to either of these two numbers

But, python reads this as

if number equals 221

or

if 780

When a number is by itself if it's not zero it's considered true, kind of a throwback to lower level languages even though it doesn't make a lot of sense on its face.

ohhhhh that makes sense, thank you so much. this is how its supposed to be right? 

if num == (221) or num == (780):

 

8 minutes ago, Logical92 said:

When a number is by itself if it's not zero it's considered true

oh i didnt know that. thanks

|:Insert something funny:|

-----------------

*******

#

Link to post
Share on other sites

21 minutes ago, adarw said:

ohhhhh that makes sense, thank you so much. this is how its supposed to be right? 

Correct, note the parentheses aren't needed.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to post
Share on other sites

Yeah, you can lighten up on the outside parentheses - they're needed for functions - like print() - and you'll use them in math as you're used to - (1+2)*3

These lines:

num = (int(input("Enter the car number model or enter 0 to quit: ")))
if num == (221) or num == (780):

Will work just fine as:

num = int(input("Enter the car number model or enter 0 to quit: "))
if num == 221 or num == 780:
Link to post
Share on other sites

Also, now that I am a real keyboard.

To be clear, what I said only applies to inside an if statement.

if statement:

Whatever is "statement" tries to be converted into a true or false.

When it comes to integers 0 == false and anything else == true.

http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/boolean.html

This link shows a lot of examples as to how things get converted, they even have one that is almost Identical to your issue!

I would recommend giving it a read if you want to better understand python booleans!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×