I have been tasked with creating a python program that will ask for user inputs and calculate monthly loan repayments, this is the formula I have to work off: Formula. Doing this was not too difficult, however the tutor asked as a bonus to try to make the user inputs 'unbreakable', meaning if any value other than the expected were input it wouldn't break the program. I thought this can easily be done with Try Except, and that's what I did, however I believe my code can be written in a much more concise manner, instead of a Try Except for every input as below:
err = "Please enter a number only!!"
while True:
try:
A = int(input("How much were you loaned? "))
except ValueError:
print(err)
continue
else:
break
while True:
try:
R = float(input("At what monthly rate interest? ")) / 100
except ValueError:
print(err)
continue
else:
break
while True:
try:
N = int(input("And how many years is the loan for? "))
except ValueError:
print(err)
continue
else:
break
RA = R * A
RN = 1 - (1 + R) ** -N
P = RA / RN
print("You will pay £", P, "yearly", "or, £", P / 12, "monthly")
I feel as if perhaps the user inputs could be put into a For Loop, or perhaps all of this into one Try Except block? After thinking about it, I put all the inputs into one Try Except block, however as soon as you fail to correctly input the expected data for the user input, it goes right back to the beginning, not to the question you were on. This is not what I want. Have a look:
err = "Please enter a number only!!"
while True:
try:
A = int(input("How much were you loaned? "))
R = float(input("At what monthly rate interest? ")) / 100
N = int(input("And how many years is the loan for? "))
except ValueError:
print(err)
continue
else:
break
RA = R * A
RN = 1 - (1 + R) ** -N
P = RA / RN
print("You will pay £", P, "yearly", "or, £", P / 12, "monthly")
How do you think I can modify this code to make it more concise, shorter, and efficient without having a Try Except for every user input?