vendredi 16 février 2018

comparison operators error (python)

I tried to create a simple calculator using python.My code is this:

def add(x, y):
   return x + y

def subtract(x, y):
 return x - y


def multiply(x, y):
 return x * y


def divide(x, y):
  return x / y
   invalid_input = True

print("Available operations")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")


choice = input("Choose one of the available operations:")
if choice >= '1' and choice <= '4':

num1 = int(input("First Number:"))
num2 = int(input("Second Number:"))

if choice == '1':
    print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
    print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
     print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
    print(num1,"/",num2,"=", divide(num1,num2))
else:
    print ("invalid number")

It works perfectly fine except of one thing. I assume the problem is here:

if choice >= '1' and choice <= '4':

When I enter 5 I get the invalid number output but when I enter a number like 12 or any number with 1,2,3 or 4 it goes inside the if and asks First number.I know how to fix it but I want to understand why this happens.Thanks in advance

Aucun commentaire:

Enregistrer un commentaire