Andrea Fox
Andrea Fox

Reputation: 53

I'm trying to get my function to perform the given parameter

I've identified input for 2 numbers and then asked for input user option. When I try to run the function using the input it ignores all of my if, elif and goes straight to the else every time.

I've tried several different ways to perform (+, -, *, /) based on input from user.

num1 = input('Select your first number')
num2 = input('Select your second number')
select_option = input('Select the option you want to perform')
option1 = int(num1)+int(num2)
option2 = int(num1)-int(num2)
option3 = int(num1)*int(num2)
option4 = int(num1)/int(num2)
def performCalculation(select_option):
    if select_option == 1:
        print(option1)
    elif select_option== 2:
        print(option2)
    elif select_option== 3:
        print(option3)
    elif select_option== 4:
        print(option4)
    else:
        print('Have a great day!')
print(performCalculation(select_option))

I'm trying to input 3 so my numbers will multiply, but eac htime it skips to print('Have a great day!')

Upvotes: 0

Views: 39

Answers (2)

Samuel Giftson
Samuel Giftson

Reputation: 116

It skips to the last line because you are not converting select option to an integer. In the performCalculation(select_option) function you are comparing a string to an integer. So it will not be equal to each other. Just make sure to convert the select_option to an integer like you did with num1 and num2.

num1 = input('Select your first number: ')
num2 = input('Select your second number: ')
select_option = input('Select the option you want to perform: ')
select_option=int(select_option)
option1 = int(num1)+int(num2)
option2 = int(num1)-int(num2)
option3 = int(num1)*int(num2)
option4 = int(num1)/int(num2)
def performCalculation(select_option):
    if select_option == 1:
        print(option1)
    elif select_option== 2:
        print(option2)
    elif select_option== 3:
        print(option3)
    elif select_option== 4:
        print(option4)
    else:
        print('Have a great day!')
print(performCalculation(select_option))

Upvotes: 1

Carcigenicate
Carcigenicate

Reputation: 45750

input returns a String, and 1 does not equal "1":

>>> 1 == "1"
False

You need to parse the returned String as an number:

select_option = int(input('Select the option you want to perform'))

Upvotes: 1

Related Questions