-2
try:
    x = int(input("Enter Your Number first = ")) or  float(input("Enter Your Number first = "))
    y = int(input("Enter Your Number first = ")) or float(input("Enter Your Number first = "))

except:
    print("Error,Invalid Input")

I know I am wrong, I was just wondering why I am unable to use it. As I am not getting any errors. When I try to put float value inside the input it returns me to my second case, which is Error, Invalid Input. I am using (or) operator so that I can validate integer and float values in one place. (or) the operator should execute the code when one of the conditions is true right? But why cannot we use Or operator with an integer? The code will work if you remove int from the code.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    If you input a float that input is passed to the int conversion and fails. Simple as that, no float conversion is ever attempted and if there was any it would actually ask for user input a second time. That is entirely unrelated to the or operator. Your code simply does something entirely different than what you think it does / want it to do. And (raise exception or somethingElse) will always raise the exception, your assumption of it attempting to do the right part is just wrong. – luk2302 Jan 23 '22 at 20:52
  • What result are you trying to get with this? Why do you try to convert the input to both int and float? – Barmar Jan 23 '22 at 20:54
  • Well trying to write just simple code to get the simple operations done like plus, minus, multiplication, divide. so that user input can be taken as float and int. Although string can do that stuff, normal users do not understand these terms, so just organize it in a proper way. – Harjot Gill Jan 23 '22 at 23:38

3 Answers3

1

While you can use the or operator to select the the first truthy value between to values (or the latter, if the first one is falsy), it doesn't work in this case because trying to convert into int a string which represents a float value doesn't return a falsy value, it raises an Exception, which is why you go into your except clause

A logically sound way to write your code would be

 x = input("Enter Your Number first = ")
 try:
     num = int(x)
 except:
     num = float(x)

Not the best way to do it, but it works

Another example:

x = 10/0 or 3 #ZeroDivisionError
x = 0/10 or 3 # x is 3 because 0 is a falsy value

Ron B.
  • 1,502
  • 2
  • 7
1

Generally, we use or case to check some conditions. For example if x<3 or x>5: This if block works of both cases. However, in your situation you are trying to get some input from user and using x = int(input("Enter Your Number first = ")) or float(input("Enter Your Number first = ")). So, when I enter a value it gets number as string and when you write int(input()) it convert it to integer and assign to x. So OR is not logically suitable for here. What will computer work it this situation? What you want to achieve? If you want take argument as float just write float(input()) because when you enter integer it can be taken as float also.

Gokhan
  • 13
  • 3
0
try:
    x = float(input("Enter Your Number first = ")) or int(input("Enter Your Number first = "))
    y = float(input("Enter Your Number first = ")) or int(input("Enter Your Number first = "))
except:
    print("Error,Invalid Input")
    operators = input("Choose operations (*, -, +, /)")
    if operators == "*":
            print(float(x*y)) or print(int(x*y))
    elif operators == "-":
            print(float(x-y)) or print(int(x-y))
    elif operators == "+":
            print(float(x+y)) or print(int(x+y))
    elif operators == "/":
            print(float(x/y)) or print(int(x/y))

well, i just figured it out,

Mohnish
  • 1,010
  • 1
  • 12
  • 20