Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User input validation & docstring update #625

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/python-beginner-projects.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 76 additions & 10 deletions projects/Loan Calculator/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,95 @@
TOTAL_PERCENT = 100
MONTH = 12


def calculate_loan_payment(principal, annual_interest_rate, months):
# Convert annual interest rate to monthly rate
monthly_interest_rate = annual_interest_rate / 12 / 100
"""
Convert annual interest rate to monthly rate
Args:
principal(float): The principal amount for the loan
annual_interest_rate(float): The interest rate for the amount annually.
months(int): The timeframe for the loan repayment in months.

Returns:
monthly_payment(float): The amount of money that needs to be paid monthly.
"""

monthly_interest_rate = annual_interest_rate / MONTH / 100

# Calculate monthly payment
if monthly_interest_rate == 0:
monthly_payment = principal / months
else:
monthly_payment = (
principal
* (monthly_interest_rate * (1 + monthly_interest_rate) ** months)
/ ((1 + monthly_interest_rate) ** months - 1)
principal
* (monthly_interest_rate * (1 + monthly_interest_rate) ** months)
/ ((1 + monthly_interest_rate) ** months - 1)
)

return monthly_payment


def get_months():
"""
This function gets the loan repayment in months.
Returns:
months(int): The timeframe for the loan repayment in months.
Error Message: Displays an error message if the user inputs are invalid.
"""
while True:
try:
months = int(input("Enter the loan term (in months): "))
if months < 0:
print("Enter a valid interest rate in percentage")
else:
return months
except ValueError:
print("Enter a valid numeric value for loan term in months")


def get_interest_rate():
"""
This function gets the interest rate for a year.
Returns:
annual_interest_rate(float): The interest rate for the amount annually.
Error Message: Displays an error message if the user inputs are invalid.
"""
while True:
try:
annual_interest_rate = float(input("Enter the annual interest rate (as a percentage): "))
if annual_interest_rate < 0 or annual_interest_rate > TOTAL_PERCENT:
print("Enter a valid interest rate in percentage")
else:
return annual_interest_rate
except ValueError:
print("Enter a valid numeric percentage for interest rate")


def get_principal():
"""
This function fetches the principal amount for the loan
Returns:
principal(float): The principal amount for the loan.
Error Message: Displays an error message if the user inputs are invalid.
"""
while True:
try:
principal = float(input("Enter the loan amount: $"))
if principal <= 0:
print("Enter a valid amount for the principal")
else:
return principal
except ValueError:
print("Invalid Input. Please enter a numeric value for the principal.")


def main():
print("Welcome to the Loan Calculator!")

# Get user input
principal = float(input("Enter the loan amount: $"))
annual_interest_rate = float(
input("Enter the annual interest rate (as a percentage): ")
)
months = int(input("Enter the loan term (in months): "))
principal = get_principal()
annual_interest_rate = get_interest_rate()
months = get_months()

# Calculate monthly payment
monthly_payment = calculate_loan_payment(principal, annual_interest_rate, months)
Expand Down
22 changes: 22 additions & 0 deletions projects/Loan Calculator/test_loancalculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
from main import calculate_loan_payment, get_months


def test_calculate_loan_payment():
# Define the expected monthly payment
expected_payment = 167.75198511394362

# Define the input parameters for the function
principal = 2000
annual_interest_rate = 1.2
months = 12

# Calculate the monthly payment using the function you want to test
monthly_payment = calculate_loan_payment(principal, annual_interest_rate, months)

# Use the assert statement to check if the calculated payment matches the expected payment
assert monthly_payment == expected_payment


if __name__ == '__main__':
test_calculate_loan_payment()