0

I'm trying to make a simple calculator with python, however, the elif blocks are not executed and it skips to the else condition even though the other conditions are met.

    select=input("Please select an operation- \n"
            "1. Add \n"
            "2. Subtract \n"
            "3. Multiply \n"
            "4. Divide \n")
    number1=input("Enter the first number: \n")
    number2=input("Enter the second number: \n")

   if select == 1:
        print(number_1, "+", number_2, "=",
        add(number_1, number_2))
  
   elif select == 2:
        print(number_1, "-", number_2, "=",
        subtract(number_1, number_2))
  
   elif select == 3:
        print(number_1, "*", number_2, "=",
        multiply(number_1, number_2))
  
   elif select == 4:
        print(number_1, "/", number_2, "=",
        divide(number_1, number_2))
   else:
        print("Invalid input")
Zainab Lawal
  • 33
  • 1
  • 9

1 Answers1

0

try this

select=input("Please select an operation- \n"
             "1. Add \n"
             "2. Subtract \n"
             "3. Multiply \n"
             "4. Divide \n")
number_1 = input("Enter the first number: \n")
number_2 = input("Enter the second number: \n")

print("selection is " + select)

if select == "1":
    print(number_1, "+", number_2, "=",
          add(number_1, number_2))
    
elif select == "2":
    print(number_1, "-", number_2, "=",
          subtract(number_1, number_2))
    
elif select == "3":
    print(number_1, "*", number_2, "=",
          multiply(number_1, number_2))
    
elif select == "4":
    print(number_1, "/", number_2, "=",
          divide(number_1, number_2))
else:
    print("Invalid input")


2 == "2" evaluates to False in python.

and the input function returns a string.

You will also have to make your divide add multiply subtract functions handle string input, or convert the string input to a number before passing it in like this

add(float(number_1), float(number_2))

(you also had number_1 in one place and number1 in another place)

Alex028502
  • 3,486
  • 2
  • 23
  • 50