I am new to python and been trying to write a small code for mortgage calculator. I use three variable namely interest, no_of_month and principal_amt and its value is taken using input function.
Below is the code for the same.
#######################
while True:
try:
no_of_month = int(input('Enter no of months you want for payment: '))
except ValueError:
print('The value you entered is not integer')
continue
else:
break
##############################
while True:
try:
interest = int(input('Enter interest you want for the loan: '))
except ValueError:
print('The value you entered is not integer')
continue
else:
break
################################
while True:
try:
principal_amt = int(input('Enter principal amount you want as loan:'))
except ValueError:
print('The value you entered is not integer')
continue
else:
break
Now the above code works fine for me, but I am not happy repeating my block of code. I wish to use function or may be something else so has to minimize my line of code.
Is there a way to define a function and call it with proper validation in place?
Thanks in advance
interest = int(input('Enter interest you want for the loan')) check_if_int_or_not(interest)int(input(question))inside the try block code.