Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 02745ac

Browse files
committed
added validation to user inputs
1 parent f6a71c0 commit 02745ac

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

projects/Loan Calculator/main.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ def main():
1919
print("Welcome to the Loan Calculator!")
2020

2121
# Get user input
22-
principal = float(input("Enter the loan amount: $"))
23-
annual_interest_rate = float(
24-
input("Enter the annual interest rate (as a percentage): ")
25-
)
26-
months = int(input("Enter the loan term (in months): "))
22+
principal = floatValidation("Enter the loan amount: $")
23+
annual_interest_rate = floatValidation("Enter the annual interest rate (as a percentage): ")
24+
months = intValidtaion("Enter the loan term (in months): ")
2725

2826
# Calculate monthly payment
2927
monthly_payment = calculate_loan_payment(principal, annual_interest_rate, months)
@@ -32,5 +30,22 @@ def main():
3230
print(f"Your monthly payment will be: ${monthly_payment:.2f}")
3331

3432

33+
def floatValidation(question):
34+
while True:
35+
try:
36+
value = float(input(question))
37+
return value
38+
except ValueError:
39+
print('Input should be a valid number')
40+
continue
41+
42+
def intValidtaion(question):
43+
while True:
44+
try:
45+
value = int(input(question))
46+
return value
47+
except ValueError:
48+
print('Input should be a valid number')
49+
3550
if __name__ == "__main__":
3651
main()

0 commit comments

Comments
 (0)