python - 在“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。但当我调用复合时,会出现相同的结果。
解决方案
这个表达式不正确:
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)
推荐阅读
- stm32 - STM32F103上升下降沿触发中断的触发电压是多少?
- javascript - 在使用 AJAX 更改另一个元素后使用 jQuery 更改元素的 CSS
- javascript - 盒子的大小不与照片大小固定
- typescript - 打字稿:覆盖超类方法而无需知道超类方法签名
- sql-server - 在 Docker 上使用 dbup 部署 SQL Server 数据库
- python - 运行 python 程序时出现 Crontab 错误
- php - 在静态函数中获取静态类的属性?
- vba - 如何对用户表单填充的数据进行顺序编号
- java - DateTime addmonths 方法是增加 1 天
- python - AttributeError:模块 'serial' 没有属性 'reset_input_buffer'