这本书说增强后应该看起来像这样。
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循环检查条目的有效性,并保持循环直到条目有效。有效的投资金额必须大于零,否则必须显示如上所示的错误消息。
使用相同的技术来添加利率和年限的数据验证。利率必须大于零且小于或等于15。年份必须大于零且小于或等于50。对于每个无效条目,显示并显示相应的错误消息。完成后,将有三个while循环和一个for循环嵌套在另一个while循环中。
我不确定将while循环放在哪里开始增强。
答案 0 :(得分:-1)
请检查以下代码,让我知道它是否有效。
#!/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!")