I just started learning python 3 recently and i have a problem of my code being too repetitive and lengthy.
i wrote a code to calculate the interest earned. I have read about using functions to simplify my code but i am not sure how to go about doing this.
Thanks for anyone who is willing to help me out! The code i have written is as follows:
print("Welcome to the monthly interest rate calculator")
original_interest = 0.0005/12
additional_food_interest_1 = 0.0055/12
additional_food_interest_2 = 0.0085/12
additional_interest_3 = 0.0090/12
additional_interest_4 = 0.0015/12
additional_interest_5 = 0.0095/12
additional_interest_6 = 0.0035/12
interest = [original_interest]
#asks user for the amount of money they have in the bank
while True:
try:
account = float(input("Enter your account balance: "))
except ValueError:
print("Try Again.")
continue
else:
break
# checks if condition 1 is met before adding either food_interest_1 or food_interest_2 variable to the list
while True:
try:
food_spending = float(input("How much did you spend on food?: "))
except ValueError:
print("Try Again.")
continue
else:
break
if food_spending >= 100 and food_spending < 200:
interest.append(additional_food_interest_1)
elif food_spending >= 200:
interest.append(additional_food_interest_2)
else:
pass
# checks if condition 2 is and if so adds additional_interest_3 to the list
while True:
try:
drinks_spending = float(input("How much did you spend on drinks?: "))
except ValueError:
print("Try Again.")
continue
else:
break
if drinks_spending >= 100:
interest.append(additional_interest_3)
else:
pass
# checks if condition 3 is met and if so adds additional_interest_4 to the list
while True:
try:
insurance_spending = float(input("How much did you spend on insurance?: "))
except ValueError:
print("Try Again.")
continue
else:
break
if insurance_spending >= 100:
interest.append(additional_interest_4)
else:
pass
# checks if condition 4 is met and if so adds additional_interest_5 to the list
while True:
try:
debitcard_spending = float(input("How much did you spend on your debit card??: "))
except ValueError:
print("Try Again.")
continue
else:
break
if debitcard_spending >= 100:
interest.append(additional_interest_5)
else:
pass
# This checks for 4 inputs from the user and if satisfies the condition, adds additional_interest_6 to the list
while True:
try:
first_receipt = float(input("Enter the amount on your first receipt: "))
except ValueError:
print("Try Again.")
continue
else:
break
while True:
try:
second_receipt = float(input("Enter the amount on your second receipt: "))
except ValueError:
print("Try Again.")
continue
else:
break
while True:
try:
third_receipt = float(input("Enter the amount on your third receipt: "))
except ValueError:
print("Try Again.")
continue
else:
break
while True:
try:
four_receipt = float(input("Enter the amount on your fourth receipt: "))
except ValueError:
print("Try Again.")
continue
else:
break
if first_receipt > 20 and second_receipt > 20 and third_receipt >20 and four_receipt > 20:
interest.append(additional_interest_6)
#calculates the total interest earned
if account <= 20000:
print("your monthly interest earned is", round(account*(sum(interest)),2))
else:
print(20000*sum(interest) + (account-20000)*original_interest)
Advertisement
Answer
This is about as concise as I can make it with a quick pass.
I refactored things so all inputs are requested first, all computation done second. Should you refactor the calculation bits into a function, that’d make the function easier to test. I also refactored the computation into a function of its own.
def prompt_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Try Again.")
def prompt_receipts():
receipts = []
while True:
receipt_value = prompt_float(
f"Enter the amount on receipt {len(receipts) + 1}, or <= 0 for no more receipts."
)
if receipt_value <= 0:
break
receipts.append(receipt_value)
if len(receipts) >= 4:
break
return receipts
def compute_interest(
*,
original_interest,
receipts,
debitcard_spending,
drinks_spending,
food_spending,
insurance_spending,
):
additional_food_interest_1 = 0.0055 / 12
additional_food_interest_2 = 0.0085 / 12
additional_interest_3 = 0.0090 / 12
additional_interest_4 = 0.0015 / 12
additional_interest_5 = 0.0095 / 12
interest = [original_interest]
if 100 <= food_spending < 200:
interest.append(additional_food_interest_1)
elif food_spending >= 200:
interest.append(additional_food_interest_2)
if drinks_spending >= 100:
interest.append(additional_interest_3)
if insurance_spending >= 100:
interest.append(additional_interest_3)
if debitcard_spending >= 100:
interest.append(additional_interest_4)
if all(receipt_value > 20 for receipt_value in receipts):
interest.append(additional_interest_5)
return interest
def main():
print("Welcome to the monthly interest rate calculator")
# Get user inputs.
account = prompt_float("Enter your account balance: ")
food_spending = prompt_float("How much did you spend on food?: ")
drinks_spending = prompt_float("How much did you spend on drinks?: ")
insurance_spending = prompt_float("How much did you spend on insurance?: ")
debitcard_spending = prompt_float(
"How much did you spend on your debit card??: "
)
receipts = prompt_receipts()
# Compute things.
original_interest = 0.0005 / 12
interest = compute_interest(
original_interest=original_interest,
receipts=receipts,
debitcard_spending=debitcard_spending,
drinks_spending=drinks_spending,
food_spending=food_spending,
insurance_spending=insurance_spending,
)
# Output things.
if account <= 20000:
print(
"your monthly interest earned is",
round(account * (sum(interest)), 2),
)
else:
print(20000 * sum(interest) + (account - 20000) * original_interest)
if __name__ == "__main__":
main()