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:
JavaScript
x
134
134
1
print("Welcome to the monthly interest rate calculator")
2
3
original_interest = 0.0005/12
4
additional_food_interest_1 = 0.0055/12
5
additional_food_interest_2 = 0.0085/12
6
additional_interest_3 = 0.0090/12
7
additional_interest_4 = 0.0015/12
8
additional_interest_5 = 0.0095/12
9
additional_interest_6 = 0.0035/12
10
11
interest = [original_interest]
12
13
14
#asks user for the amount of money they have in the bank
15
while True:
16
try:
17
account = float(input("Enter your account balance: "))
18
except ValueError:
19
print("Try Again.")
20
continue
21
else:
22
break
23
24
# checks if condition 1 is met before adding either food_interest_1 or food_interest_2 variable to the list
25
while True:
26
try:
27
food_spending = float(input("How much did you spend on food?: "))
28
except ValueError:
29
print("Try Again.")
30
continue
31
else:
32
break
33
34
if food_spending >= 100 and food_spending < 200:
35
interest.append(additional_food_interest_1)
36
elif food_spending >= 200:
37
interest.append(additional_food_interest_2)
38
else:
39
pass
40
41
42
# checks if condition 2 is and if so adds additional_interest_3 to the list
43
while True:
44
try:
45
drinks_spending = float(input("How much did you spend on drinks?: "))
46
except ValueError:
47
print("Try Again.")
48
continue
49
else:
50
break
51
52
if drinks_spending >= 100:
53
interest.append(additional_interest_3)
54
else:
55
pass
56
57
# checks if condition 3 is met and if so adds additional_interest_4 to the list
58
while True:
59
try:
60
insurance_spending = float(input("How much did you spend on insurance?: "))
61
except ValueError:
62
print("Try Again.")
63
continue
64
else:
65
break
66
67
if insurance_spending >= 100:
68
interest.append(additional_interest_4)
69
else:
70
pass
71
72
73
# checks if condition 4 is met and if so adds additional_interest_5 to the list
74
while True:
75
try:
76
debitcard_spending = float(input("How much did you spend on your debit card??: "))
77
except ValueError:
78
print("Try Again.")
79
continue
80
else:
81
break
82
83
if debitcard_spending >= 100:
84
interest.append(additional_interest_5)
85
else:
86
pass
87
88
# This checks for 4 inputs from the user and if satisfies the condition, adds additional_interest_6 to the list
89
90
while True:
91
try:
92
first_receipt = float(input("Enter the amount on your first receipt: "))
93
except ValueError:
94
print("Try Again.")
95
continue
96
else:
97
break
98
99
while True:
100
try:
101
second_receipt = float(input("Enter the amount on your second receipt: "))
102
except ValueError:
103
print("Try Again.")
104
continue
105
else:
106
break
107
108
while True:
109
try:
110
third_receipt = float(input("Enter the amount on your third receipt: "))
111
except ValueError:
112
print("Try Again.")
113
continue
114
else:
115
break
116
117
while True:
118
try:
119
four_receipt = float(input("Enter the amount on your fourth receipt: "))
120
except ValueError:
121
print("Try Again.")
122
continue
123
else:
124
break
125
126
if first_receipt > 20 and second_receipt > 20 and third_receipt >20 and four_receipt > 20:
127
interest.append(additional_interest_6)
128
129
#calculates the total interest earned
130
if account <= 20000:
131
print("your monthly interest earned is", round(account*(sum(interest)),2))
132
else:
133
print(20000*sum(interest) + (account-20000)*original_interest)
134
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.
JavaScript
1
93
93
1
def prompt_float(prompt):
2
while True:
3
try:
4
return float(input(prompt))
5
except ValueError:
6
print("Try Again.")
7
8
9
def prompt_receipts():
10
receipts = []
11
while True:
12
receipt_value = prompt_float(
13
f"Enter the amount on receipt {len(receipts) + 1}, or <= 0 for no more receipts."
14
)
15
if receipt_value <= 0:
16
break
17
receipts.append(receipt_value)
18
if len(receipts) >= 4:
19
break
20
return receipts
21
22
23
def compute_interest(
24
*,
25
original_interest,
26
receipts,
27
debitcard_spending,
28
drinks_spending,
29
food_spending,
30
insurance_spending,
31
):
32
additional_food_interest_1 = 0.0055 / 12
33
additional_food_interest_2 = 0.0085 / 12
34
additional_interest_3 = 0.0090 / 12
35
additional_interest_4 = 0.0015 / 12
36
additional_interest_5 = 0.0095 / 12
37
interest = [original_interest]
38
if 100 <= food_spending < 200:
39
interest.append(additional_food_interest_1)
40
elif food_spending >= 200:
41
interest.append(additional_food_interest_2)
42
if drinks_spending >= 100:
43
interest.append(additional_interest_3)
44
if insurance_spending >= 100:
45
interest.append(additional_interest_3)
46
if debitcard_spending >= 100:
47
interest.append(additional_interest_4)
48
if all(receipt_value > 20 for receipt_value in receipts):
49
interest.append(additional_interest_5)
50
return interest
51
52
53
def main():
54
print("Welcome to the monthly interest rate calculator")
55
56
# Get user inputs.
57
58
account = prompt_float("Enter your account balance: ")
59
food_spending = prompt_float("How much did you spend on food?: ")
60
drinks_spending = prompt_float("How much did you spend on drinks?: ")
61
insurance_spending = prompt_float("How much did you spend on insurance?: ")
62
debitcard_spending = prompt_float(
63
"How much did you spend on your debit card??: "
64
)
65
receipts = prompt_receipts()
66
67
# Compute things.
68
69
original_interest = 0.0005 / 12
70
interest = compute_interest(
71
original_interest=original_interest,
72
receipts=receipts,
73
debitcard_spending=debitcard_spending,
74
drinks_spending=drinks_spending,
75
food_spending=food_spending,
76
insurance_spending=insurance_spending,
77
)
78
79
# Output things.
80
81
if account <= 20000:
82
print(
83
"your monthly interest earned is",
84
round(account * (sum(interest)), 2),
85
)
86
else:
87
print(20000 * sum(interest) + (account - 20000) * original_interest)
88
89
90
if __name__ == "__main__":
91
main()
92
93