首页 > 解决方案 > 在“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,年数为六,简单的输入为 6500。但当我调用复合时,会出现相同的结果。

标签: pythonboolean

解决方案


这个表达式不正确:

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)

推荐阅读