In: Computer Science
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This assignment will give you experience on the use of the while loop and the for loop.
You will use both selection (if) and repetition (while, for) in this assignment. Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following:
1. Ask the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added to the balance.
2. Ask the user for the amount withdrawn from the account during the month. (Do not accept negative numbers.) This amount should be subtracted from the balance.
3. Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by 12. Multiply the monthly interest rate by the balance and add the result to the balance. After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned. Note: If a negative balance is calculated at any point, a message should be displayed indicating the account has been closed and the loop should terminate.
#This program collects some data from the
#user about operations done to a saving
#account then, it presents the information
#derived from the data
totalDeposit = 0.0
totalWithdraw = 0.0
earnedInterest= 0.0
#annual Interest input
annualInterest = float(input("Enter the Annual interest Rate: "))
#input balance amount
balance = float(input("Enter the starting balance: "))
while balance<0:
print("Negative values are not allowed")
balance = float(input("Enter the starting balance: "))
#input number of months
months = int(input("Enter the number of months: "))
while months<=0:
print("months can not be 0 or less than zero")
months = int(input("Enter the number of months: "))
#Loop over all the monts to get the data
for i in range(1,months+1):
# Get the deposited value during that month.
deposited = float(input(f"Enter the deposited value during month {i}: "))
while deposited<0:
print("deposit value can not be less than zero")
deposited = float(input(f"Enter the deposited value during month {i}: "))
totalDeposit += deposited
balance += deposited
#Get the withdrawn value during that month
withdrawn = float(input(f"Enter the withdrawn value during month {i}: "))
while withdrawn<0:
print("withdrawn value can not be less than zero")
deposited = float(input(f"Enter the withdrawn value during month {i}: "))
totalWithdraw += withdrawn
balance -= withdrawn
#Negative balance close the program.
if balance <0:
print("Sorry your account is closed due to negative balance")
break
#Calculate value due to the interest rate
else:
earnedInterest += (annualInterest/12) * balance
#monthly interest rate
balance += (annualInterest/12) * balance
print("Statistics: ")
print("The Ending Balance: ",balance)
print("Total Deposit: ",totalDeposit)
print("Total Withdrawn: ",totalWithdraw)
print("Earned Interest: ",earnedInterest)