我正在尝试让我的函数执行给定的参数

时间:2019-07-06 23:22:27

标签: python-3.x

我确定了2个数字的输入,然后要求输入用户选项。当我尝试使用输入来运行该函数时,它将忽略我的所有if,elif并每次都直接进入else。

根据用户的输入,我尝试了几种不同的执行方式(+,-,*,/)。

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))

我正在尝试输入3,所以我的数字会相乘,但是每次都不会打印(“祝您有美好的一天!”)

2 个答案:

答案 0 :(得分:1)

input返回一个字符串,并且1不等于"1"

>>> 1 == "1"
False

您需要将返回的String解析为数字:

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

答案 1 :(得分:1)

它跳到最后一行,因为您没有将select选项转换为整数。在performCalculation(select_option)函数中,您正在将字符串与整数进行比较。所以它不会彼此相等。只需确保将select_option转换为整数,就像使用num1和num2一样。

helm get values