'if/elif/else'语句中调用的代码不正确



我正在制作一个利息计算器,可以计算复利和单利。但是,if 语句始终运行单利脚本,而不考虑输入。

我尝试将变量更改为字符串、整数和浮点数。我尝试更改变量名称,尝试完全删除第一个代码块。这到底是怎么回事???

start = input("simple or compound: ")
if start == "simple" or "Simple":
    a = float(input('Starting balance: '))
    b = float(input('Rate: '))
    c = int(input('Years: '))
    final = int(a+((a*b*c)/100))
    print(final)
elif start == "compound" or "Compound":
    d = float(input('Starting balance: '))
    e = float(input('Rate: '))
    f = int(input('Years: '))
    final2 = int(d*(1+(e/100))**f)
    print(final2)
else:
    d = float(input('Starting balance: '))
    e = float(input('Rate: '))
    f = int(input('Years: '))
    final3 = int(d*(1+(e/100))**f)
    print(final3)

如果我输入起始余额为 5000,费率为 5,年输入为 6 简单,则给出 6500。但是当我调用化合物时,也会发生相同的结果。

此表达式不正确:

start == "simple" or "Simple"

应该是

start == "simple" or start "Simple"

下面的代码有效:

start = input("simple or compound: ")
if start == "simple" or start == "Simple":
    # print("simple")
    a = float(input('Starting balance: '))
    b = float(input('Rate: '))
    c = int(input('Years: '))
    final = int(a+((a*b*c)/100))
    print(final)
elif start == "compound" or start == "Compound":
    # print("compound")
    d = float(input('Starting balance: '))
    e = float(input('Rate: '))
    f = int(input('Years: '))
    final2 = int(d*(1+(e/100))**f)
    print(final2)
else:
    # print("unknown")
    d = float(input('Starting balance: '))
    e = float(input('Rate: '))
    f = int(input('Years: '))
    final3 = int(d*(1+(e/100))**f)
    print(final3)

由于运算符优先级

if start == "simple" or "Simple"

被评估为

if (start == "simple") or "Simple"

如果用户输入"简单",则(...)部分为 True,但"简单"部分始终为 True。

最新更新