the book says it should look like this after enhancing.
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)?
This is what I have in my program
#!/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!")
add data validation for the monthly investment entry. Use a while loop to check the validity of the entry and keep looping until the entry is valid. To be valid, the investment amount must be greater than zero.If it isn't, an error message like the firt one shown above should be displayed.
Use the same technique to add data validation for the interest rate and years. The interest rate must be greater than zero and less than or equal to 15. And the years must be greater than zero and less than or equal to 50. For each invalid entry, display and appropriate error message. When you're finihed, you'll have three while loops and a for loop nested within an other while loop.
I'm not quite sure where to put my while loops to begin enhancing.
Please check below code, let me know if it works.
#!/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!")