如何解决我正在学习基本的Python我无法执行此代码
我正在学习基本的Python。我无法执行此代码:
n=0
current_savings = 0.0
while current_savings<7500.0:
current_savings = 1000.0 + (4/12/100*current_savings)
print(current_savings)
n = n+1
print(n)
我在做什么错?如果我不除以12和100,我的代码将起作用。
解决方法
我相信您正在尝试在每次迭代后更新current_ Savings的值:
n=0
current_savings = 0.00
while current_savings<7500.0:
current_savings = current_savings + (1000.0 + (4/12/100*current_savings))
print(current_savings)
n = n+1
print(n)
但是您当前正在做的是将表达式1000.0 + (4/12/100*current_savings)
的值(即1003.34)重复分配给current_ Savings。您永远不会达到7500.0的终止值,这将导致while循环无限运行,因为current_savings始终为1003.34。
我认为这是无限循环的。
n=0
current_savings = 0.0
while current_savings<7500.0:
current_savings = 1000.0 + (4/12/100*current_savings)
print(n,current_savings)
n = n+1
if n==5:
break
如以下输出所示:
0 1000.0
1 1003.3333333333334
2 1003.3444444444444
3 1003.3444814814815
4 1003.3444816049383
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。