Question

In: Computer Science

I am currently having trouble understanding/finding the errors in this python code. I was told that...

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.

Solutions

Expert Solution

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!


Related Solutions

I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
I am having trouble understanding what value to use for each function. The question is: "Tom...
I am having trouble understanding what value to use for each function. The question is: "Tom plans to save $88 a month, starting today, for 22 years. Dean plans to save $88 a month for 22 years, starting one month from today. Both Tom and Dean expect to earn an average return o 5.27 percent APR on thier savings and both will make the same number of deposits. At the end of the 22 years, how much more (in $)...
Develop the following code for quiz (i am getting some errors)in python in such a manner...
Develop the following code for quiz (i am getting some errors)in python in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o...
Sherald is having trouble finding a procedural code in the Alphabetic Index for removal of a...
Sherald is having trouble finding a procedural code in the Alphabetic Index for removal of a cataract. What are some options and/or alternative ways she can perform an Alphabetic Index search?
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it....
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it. The purpose of this experiment was to investigate the catalytic performance of solid acid catalyst developed from modification of coal combustion fly ash to synthesize methyl 4-aminobenzoate (MAB) by Fischer esterification between 4-aminobenzoic acid and methanol. Upon acid modification of fly ash, surface acidity of solid acid catalyst (SAC) is greatly enhanced as a result of increased surface area, augmented silica content, and fabricated...
I was able to calculate (a) but I am having trouble with the calculations of (b)....
I was able to calculate (a) but I am having trouble with the calculations of (b). Thanks! The following are New York closing rates for A$/US$ and $/£:                                     A$/$ = 1.5150;               $/£ = $1.2950             (a) Calculate the cross rate for £ in terms of A$ (A$/£).             (b) If £ is trading at A$1.95/£ in London (cross market) on the same day, is there an arbitrage opportunity?  If so, show how arbitrageurs with £ could profit from this opportunity and calculate the arbitrage...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
I am getting confused on finding the ethical challanges to this question. I am understanding but...
I am getting confused on finding the ethical challanges to this question. I am understanding but second guessing my answers when it come to the elements and the Ethical Code Standards. 1. Identify an ethical challenge for each element (a-d) of Dr Lux advertising plan. Which General Principles and Ethical Code Standards best help understand why these elements may create ethical problems? Explain why. Case 5. Web-Based Advertising. Dr. Lux, an applied developmental psycholgoist, has created his own community consulation...
I am having trouble understanding the following: When interest rates are low (decreasing) what impact does...
I am having trouble understanding the following: When interest rates are low (decreasing) what impact does it have on the profitability of commercial banks, finance companies, insurance companies, security firms, and mutual funds? Additionally, what exactly could they do to combat this? Short simple explanations work! Thanks.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT