In: Computer Science
I am currently having trouble understanding/finding the errors
in this python code. I was told that there are 5 errors to
fix.
Code:
#!/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.")) #Error 1 extra ")"
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!")
Any help on finding and explaining the errors would be very appreciated.
Five errors fixed and marked in the code:
CODE:
choice = "y"
while choice == "y":
# get monthly investment
monthly_investment = float(input(f"Enter monthly investment
(0-1000):\t"))
#Error 3: you had typed 100 here in the if statement in place of
1000
if not(monthly_investment > 0 and monthly_investment <=
1000):
print(f"Entry must be greater than 0 and less than or equal to
1000. "
"Please start over.") #Error 1 extra ")": this is because there was
an extra parenthesis which
#you might have entered by mistake
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
#Error 2: you had entered 15 here in the if statement in place of
50
years = int(input(f"Enter number of years (0-50):\t\t"))
if not years > 0 or not years <= 50:
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
#Error 4: The future value should initially be the investment
future_value = monthly_investment
for i in range(1, months):
#Error 4: future this line initializes the future_value to the
initial amount after every loop
#future_value = monthly_investment
#Error 5: interest = future_value * rate / 100 which was
missing
monthly_interest = future_value * monthly_interest_rate / 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 IMAGES AND OUTPUT:
_________________________________________________
Feel free to ask any questions in the comments section
Thank You!