【问题标题】:Python calculator only returning else statement even though the if or elif is true即使 if 或 elif 为真,Python 计算器也只返回 else 语句
【发布时间】:2022-01-25 17:00:39
【问题描述】:

这是我的代码,为什么即使我输入了有效的操作,它仍然会返回 else 值?

num1 = float(input("Choose a number: "))
op = input("Choose an operation: ")
num2 = float(input("Choose a second number: "))


if op == "+":
    print(num1 + num2)
elif op == "-":
    print(num1 - num2)
elif op == "*":
    print(num1 * num2)
elif op == "/":
    print(num1 / num2)
else:
    print("Invalid Operator")

【问题讨论】:

标签: if-statement input calculator


【解决方案1】:

您的程序似乎是正确的,并且它产生了正确的输出。您的输入可能有问题。这是您的程序输入和输出的示例。

Choose a number: 5
Choose an operation: +
Choose a second number: 3
8.0

【讨论】:

    猜你喜欢
    • 2017-07-03
    • 2020-06-07
    • 2019-05-20
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多