"if elif" statement dependent on input won't run after placing my input
My problem is the script ends immediately after I enter an input.
def add(var1, var2): print(var1 + var2) def times(var1, var2): print(var1*var2) x = input("press 1 to add, press 2 to multiply: ") if x == 1: print("what two number do you want to add?") a = input("input first number: ") b = input("input second number: ") add(a, b) elif x == 2: print("what two number do you want to multiply?") a = input("input first number: ") b = input("input second number: ") times(a, b)
I want the script to run the if-elif statement which is dependent on the entered input.
The input
function returns a string. You should therefore either compare its returning value to a string instead of an integer:
if x == '1': ... if x == '2':
or convert the returning value to an integer first:
x = int(input("press 1 to add, press 2 to multiply: "))
The input will be a string not a number, and 2 != '2'. You need to convert the input to an integer before trying to use it.
input()
will return str
, you need to convert it to int
before calculating.
x = input("press 1 to add, press 2 to multiply: ")