In: Computer Science
In this exercise, you will test and debug a Future Value Program. |
|||||||||||||||||||||||||||||||||
The pseudocode of the program is as follows: Display user message WHILE user wants to continue get monthly investment, yearly interest rate, and years convert yearly interest rate to monthly interest rate convert years to months set the future value to zero FOR each month add monthly investment amount to future value calculate interest for month add interest to future value display future value ask if user wants to continue Display end message |
|||||||||||||||||||||||||||||||||
Test Plan
. |
|||||||||||||||||||||||||||||||||
Requirements:
|
|||||||||||||||||||||||||||||||||
(The source code in the lp_wk08_future_value.py) #!/usr/bin/env python3 choice = "y" while choice == "y": # get monthly investment monthly_investment = float(input(f"Enter monthly investment (0-1000):\t")) if not(monthly_investment > 0 and monthly_investment <= 100): print(f"Entry must be greater than 0 and less than or equal to 1000. " "Please start over.")) continue # get yearly interest rate yearly_interest_rate = float(input(f"Enter yearly interest rate (0-15):\t")) if not(yearly_interest_rate > 0 and yearly_interest_rate <= 15): print(f"Entry must be greater than 0 and less than or equal to 15. " "Please start over.") continue # get years years = int(input(f"Enter number of years (0-50):\t\t")) if not years > 0 or not years <= 15: print(f"Entry must be greater than 0 and less than or equal to 15. " "Please start over.") continue # convert yearly values to monthly values monthly_interest_rate = yearly_interest_rate / 12 months = years * 12 # calculate future value future_value = 0.0 for i in range(1, months): future_value = monthly_investment monthly_interest = future_value * monthly_interest_rate future_value += monthly_interest # display future value print() 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!") |
##IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE IF YOU HAVE DOUBTS KINDLY COMMENT.
# I HAVE COMMENTED OUT BY THE SIDE OF THE CODE WHERE THERE IS ERROR
CODE:
choice = "y"
while choice == "y" or choice == "Y": #error here choice can be ==
"Y" as well
# get monthly investment
monthly_investment = float(input(f"Enter monthly investment
(0-1000):\t"))
if not(monthly_investment > 0 and monthly_investment <=
1000): #here is an error!
print(f"Entry must be greater than 0 and less than or equal to
1000. "
"Please start over.") #here is an error!
continue
# get yearly interest rate
yearly_interest_rate = float(input(f"Enter yearly interest rate
(0-15):\t"))
if not(yearly_interest_rate > 0 and yearly_interest_rate <=
15):
print(f"Entry must be greater than 0 and less than or equal to 15.
"
"Please start over.")
continue
# get years
years = int(input(f"Enter number of years (0-50):\t\t"))
if not years > 0 or not years <= 50: #error here! 15 will be
50
print(f"Entry must be greater than 0 and less than or equal to 50.
" #error here! 15 is 50 here
+"Please start over.")
continue
# convert yearly values to monthly values
monthly_interest_rate = yearly_interest_rate / 12
months = years * 12
# calculate future value
future_value = 0.0
for i in range(1, months+1): #error here! change to month+1
future_value += monthly_investment #error here! should be
future_value = future_value + monthly_investment
monthly_interest = future_value * monthly_interest_rate/100 #error
here! interest is always divided by 100
future_value += monthly_interest
# display future value
print()
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!")
CODE PREVIEW:
CODE OUTPUT: