Skip to content

Instantly share code, notes, and snippets.

@cagatayuresin
Created December 21, 2023 15:00
Show Gist options
  • Save cagatayuresin/3f50a2236d7efbd3526276d58890efc0 to your computer and use it in GitHub Desktop.
Save cagatayuresin/3f50a2236d7efbd3526276d58890efc0 to your computer and use it in GitHub Desktop.

Interest Calculator

Python 3.10Python 3.11

Calculates the total amount of money and the interest earned based on the given capital, interest rate, and maturity period.

Usage

./interest_calculator.py -c 14000 -r 0.5 -t "day" -m 100
./interest_calculator.py --capital 14000 --interest_rate 0.5 --maturity_type day --maturity 100
./interest_calculator.py
python interest_calculator.py -c 14000 -r 0.5 -t "day" -m 100
python interest_calculator.py --capital 14000 --interest_rate 0.5 --maturity_type day --maturity 100
python interest_calculator.py
python3 interest_calculator.py -c 14000 -r 0.5 -t "day" -m 100
python3 interest_calculator.py --capital 14000 --interest_rate 0.5 --maturity_type day --maturity 100
python3 interest_calculator.py

Note: If you do not enter any arguments, it will ask for the rest of the program.

Output

|_|___| |_ ___ ___ ___ ___| |_
| |   |  _| -_|  _| -_|_ -|  _|
|_|_|_|_| |___|_| |___|___|_|
 ___ ___| |___ _ _| |___| |_ ___ ___
|  _| .'| |  _| | | | .'|  _| . |  _|
|___|__,|_|___|___|_|__,|_| |___|_|

๐Ÿ’ธ Capital                           : 14000.0
๐Ÿ“ˆ Interest % (year)                 : 0.5
๐Ÿ“… Maturity Type ('day', month, year): day
โฒ  Maturity (day)                   : 100

๐Ÿ’ธ New Capital -> 14019.18, ๐Ÿ’ฐ Interest -> 19.18

Press 'q' to quit, 'Enter' to continue...
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Cagatay URESIN <[email protected]> Github: cagatayuresin
import argparse
LOGO = """
|_|___| |_ ___ ___ ___ ___| |_
| | | _| -_| _| -_|_ -| _|
|_|_|_|_| |___|_| |___|___|_|
___ ___| |___ _ _| |___| |_ ___ ___
| _| .'| | _| | | | .'| _| . | _|
|___|__,|_|___|___|_|__,|_| |___|_|
"""
def calculateInterest(
capital: float,
interest_rate: float,
maturity: int = 30,
maturity_type: ("year", "month", "day") = "day",
) -> (float, float):
"""
The `calculateInterest` function calculates the total amount of money and the interest earned based
on the given capital, interest rate, and maturity period.
:param capital: The initial amount of money or capital on which the interest is calculated
:type capital: float
:param interest_rate: The `interest_rate` parameter represents the interest rate for calculating the
interest. It is a float value representing the percentage interest rate. For example, if the
interest rate is 5%, you would pass 5 as the value for `interest_rate`
:type interest_rate: float
:param maturity: The `maturity` parameter represents the time period for which the interest is
calculated. It is an integer value that represents the number of years, months, or days depending on
the value of the `maturity_type` parameter. By default, the `maturity` is set to 30,, defaults to 30
:type maturity: int (optional)
:param maturity_type: The `maturity_type` parameter is used to specify the unit of time for the
maturity period. It can be one of the following values: "year", "month", or "day", defaults to day
:type maturity_type: ("year", "month", "day") (optional)
:returns (new_capital, interest)
:type new_capital: float
:type interest: float
:type return: tuple
"""
# The `match` statement is a new feature introduced in Python 3.10 called pattern matching. It
# allows you to match a value against a set of patterns and execute the corresponding code block
# based on the matched pattern.
match maturity_type.strip().lower():
case "day":
pass
# The code `case "month": maturity *= 30` is multiplying the `maturity` value by 30 if the
# `maturity_type` is "month". This is done to convert the maturity period from months to days,
# as the interest calculation is based on the number of days. By multiplying the `maturity`
# value by 30, it effectively converts the months to days.
case "month":
maturity *= 30
# The code `case "year": maturity *= 365` is multiplying the `maturity` value by 365 if the
# `maturity_type` is "year". This is done to convert the maturity period from years to days,
# as the interest calculation is based on the number of days. By multiplying the `maturity`
# value by 365, it effectively converts the years to days.
case "year":
maturity *= 365
# The line `interest = round(capital * interest_rate / 100 * maturity / 365, 2)` is calculating
# the interest earned based on the given capital, interest rate, and maturity period.
interest = round(capital * interest_rate / 100 * maturity / 365, 2)
return capital + interest, interest
def main():
# The code block `parser = argparse.ArgumentParser(description="Interest Calculator")` creates an
# ArgumentParser object from the argparse module. The ArgumentParser object is used to define the
# command-line arguments that the script can accept.
parser = argparse.ArgumentParser(description="Interest Calculator")
parser.add_argument("--capital", "-c", type=float, help="Capital value")
parser.add_argument(
"--interest_rate", "-r", type=float, help="Annual interest rate as %"
)
parser.add_argument("--maturity", "-m", type=int, help="Maturity")
parser.add_argument(
"--maturity_type",
"-t",
choices=["day", "month", "year"],
help="Maturity type: day, month, year",
)
args = parser.parse_args()
print(LOGO)
maturity_type_ask = "๐Ÿ“… Maturity Type ('day', month, year)"
capital_ask = "๐Ÿ’ธ Capital"
capital_ask += (len(maturity_type_ask) - len(capital_ask)) * " " + ": "
interest_rate_ask = "๐Ÿ“ˆ Interest % (year)"
interest_rate_ask += (len(maturity_type_ask) - len(interest_rate_ask)) * " " + ": "
maturity_type_ask += ": "
# The code block is checking if the `capital` argument is provided when running the script from
# the command line. If the `capital` argument is provided, it assigns the value of the `capital`
# argument to the `capital` variable and prints it. If the `capital` argument is not provided, it
# prompts the user to enter the value of the `capital` by using the `input()` function and assigns
# the user input to the `capital` variable.
if args.capital:
capital = args.capital
print(capital_ask + str(capital))
else:
capital = float(input(capital_ask))
# This code block is checking if the `interest_rate` argument is provided when running the script
# from the command line.
if args.interest_rate:
interest_rate = args.interest_rate
print(interest_rate_ask + str(interest_rate))
else:
interest_rate = float(input(interest_rate_ask))
# The code block is checking if the `maturity_type` argument is provided when running the script
# from the command line.
if args.maturity_type:
maturity_type = args.maturity_type
print(maturity_type_ask + maturity_type)
else:
maturity_type = input(maturity_type_ask)
maturity_ask = f"โฒ Maturity ({maturity_type})"
maturity_ask += (len(maturity_type_ask) - len(maturity_ask) - 1) * " " + ": "
# This code block is checking if the `maturity` argument is provided when running the script from
# the command line.
if args.maturity:
maturity = args.maturity
print(maturity_ask + str(maturity))
else:
maturity = float(input(maturity_ask))
new_capital, interest = calculateInterest(
capital, interest_rate, maturity, maturity_type
)
print(f"\n๐Ÿ’ธ New Capital -> {new_capital}, ๐Ÿ’ฐ Interest -> {interest}\n")
# The code `if input("Press 'q' to quit, 'Enter' to continue... ") in ("q", "Q", "n", "N"):
# exit(0)` is checking if the user's input is one of the specified values ("q", "Q", "n", "N"). If
# the user enters any of these values, the program will exit with a status code of 0, terminating
# the execution. This allows the user to quit the program by entering one of the specified values.
if input("Press 'q' to quit, 'Enter' to continue... ") in ("q", "Q", "n", "N"):
exit(0)
# The `main()` function is the entry point of the program. It displays the program's logo, prompts
# the user to input the capital, interest rate, maturity type, and maturity period. It then calls
# the `calculateInterest()` function to calculate the new capital and interest based on the user's
# inputs. Finally, it prints the new capital and interest and waits for user input before calling
# `main()` again to restart the program.
main()
# The `if __name__ == "__main__"` condition is used to check if the current script is being run as the
# main module or if it is being imported as a module into another script.
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment