Python 3.x murach python循环增强挑战
书上说增强后应该是这样Python 3.x murach python循环增强挑战,python-3.x,pseudocode,Python 3.x,Pseudocode,书上说增强后应该是这样 Welcome to the future value calculator enter monthly investment: 0 Entry must be greater than 0 . Please try again. enter monthly investment: 100 enter yearly interest rate: 16 Entry must be greater than 0 and less than or equal to 15. P
Welcome to the future value calculator
enter monthly investment: 0
Entry must be greater than 0 . Please try again.
enter monthly investment: 100
enter yearly interest rate: 16
Entry must be greater than 0 and less than or equal to 15. Please try again.
enter yearly interest rate: 12
Enter number of years: 100
Entry must be greater than 0 and less than or equal to 50
please try again.
enter number of years: 10
Year = 1 Future Value = 1280.93
Year = 2 Future VAlue = 2724.32
Year = 3 Future Value = 4350.76
Year = 4 Future Value = 6183.48
Year = 5 Future Value = 8248.64
Year = 6 Future Value = 10575.7
Year = 7 Future Value = 13197.9
Year = 8 Future Value = 16152.66
Year = 9 Future Value = 19482.15
Year = 10 Future Value = 23233.91
Continue (y/n)?
这就是我在我的程序中所做的
#!/usr/bin/env python3
# display a welcome message
print("Welcome to the Future Value Calculator")
print()
choice = "y"
while choice.lower() == "y":
# get input from the user
monthly_investment = float(input("Enter monthly investment:\t"))
yearly_interest_rate = float(input("Enter yearly interest rate:\t"))
years = int(input("Enter number of years:\t\t"))
# convert yearly values to monthly values
monthly_interest_rate = yearly_interest_rate / 12 / 100
months = years * 12
# calculate the future value
future_value = 0
for i in range(months):
future_value += monthly_investment
monthly_interest_amount = future_value * monthly_interest_rate
future_value += monthly_interest_amount
# display the result
print("Future value:\t\t\t" + str(round(future_value, 2)))
print()
# see if the user wants to continue
choice = input("Continue (y/n)? ")
print()
print("Bye!")
我不太确定我的while循环应该放在哪里开始增强 请检查下面的代码,让我知道它是否有效
#!/usr/bin/env python3
# display a welcome message
print("Welcome to the Future Value Calculator")
print()
choice = "y"
while choice.lower() == "y":
# get input from the user
while True:
monthly_investment = float(input("Enter monthly investment: "))
if monthly_investment <= 0 :
print("Entry must be greater than 0 . Please try again.")
continue
else:
break
while True:
yearly_interest_rate = float(input("Enter yearly interest rate: "))
if yearly_interest_rate <= 0 or yearly_interest_rate >= 15:
print("Entry must be greater than 0 and less than or equal to 15. Please try again.")
continue
else:
break
while True:
years = int(input("Enter number of years: "))
if years <= 0 or years >= 50:
print("Entry must be greater than 0 and less than or equal to 50")
continue
else:
break
# convert yearly values to monthly values
monthly_interest_rate = yearly_interest_rate / 12 / 100
months = years * 12
# calculate the future value
future_value = 0
for i in range(1,months+1):
future_value += monthly_investment
monthly_interest_amount = future_value * monthly_interest_rate
future_value += monthly_interest_amount
if i % 12 ==0:
# display the result
print("Year = {0} Future value:\t{1}".format(i//12,str(round(future_value, 2))))
# see if the user wants to continue
choice = input("Continue (y/n)? ")
print()
print("Bye!")
#/usr/bin/env蟒蛇3
#显示欢迎信息
打印(“欢迎使用未来值计算器”)
打印()
choice=“y”
while choice.lower()=“y”:
#从用户处获取输入
尽管如此:
月度投资=浮动(输入(“输入月度投资:”)
如果你是每月投资的话,请你向我解释一下你编写代码的心理过程。我很高兴它起了作用,但我想有一个更好的理解,这样在将来,如果出现这样的问题,我可以有一些经验来处理它自己。非常感谢。正如为什么break命令将其引导到下一个语句/问题,而不是返回到初始语句/问题一样,Truesure首先,在编写此程序时没有考虑性能。我使用了很多while循环,但是使用单个while循环也可以达到相同的目的。我们有一个无限while循环和许多嵌套的无限while循环。让我们来讨论嵌套while循环。记住,当您处于内部while循环的范围内时,在这种情况下,break总是基于封闭缩进终止。在我们的例子中,这是一个while循环。因此,它与电流while循环断开。其他人也一样。现在让我们讨论for循环,我添加的条件是基于月份的。因此,12的倍数将被计算为年数,仅显示每年的奇特产量。