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

时间:2019-07-19 02:00:36

标签: python boolean

我正在制作一个能计算复利和单利的利息计算器。但是,无论输入如何,if语句始终运行简单的兴趣脚本。

我尝试将变量更改为字符串,整数和浮点数。我尝试更改变量名,尝试完全删除第一段代码。到底是怎么了?

$cacheFile = '/home/directories/php/wwwrun/cache.html';

如果我将“起始余额”输入为5000,将“比率”输入为5,将年数输入为“ 6”,则得出的数字为6500。

2 个答案:

答案 0 :(得分:0)

此表达式不正确:

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)

答案 1 :(得分:0)

由于运算符优先级

if start == "simple" or "Simple"

被评估为

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

如果用户输入“ simple”,则(...)部分为True,但是“ Simple”部分始终为True。