python解释器不运行elif块

x4shl7ld  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(367)

此问题已在此处找到答案

如何将输入读取为数字((10个答案)
12小时前关门。
我试图用python制作一个简单的计算器,但是,不会执行elif块,即使满足其他条件,它也会跳到else条件。

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

ikfrs5lh1#

试试这个

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"` 评估为 `False` 在python中。
输入函数返回一个字符串。
你还必须做出决定
divide `add` multiply `subtract` 函数处理字符串输入,或者在像这样传入字符串输入之前将其转换为数字

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

(你也有 `number_1` 在一个地方 `number1` (在另一个地方)

相关问题