commit 78be05027f81a57d4a5ed8438292d08364c9c03e Author: Jens Tirsvad Nielsen Date: Sat Oct 3 12:27:36 2026 +0800 Add tip calculator project structure and documentation diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f91d628 --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# Byte-compiled / cache +__pycache__/ +*.py[cod] +*$py.class + +# Virtual environments +.venv/ +venv/ +env/ + +# Secrets +.env + +# Packaging / build +build/ +dist/ +*.egg-info/ +.eggs/ + +# Testing / tooling +.pytest_cache/ +.coverage +htmlcov/ +.mypy_cache/ +.ruff_cache/ + +# IDEs / OS +.idea/ +.vscode/ +.DS_Store +Thumbs.db diff --git a/README.md b/README.md new file mode 100644 index 0000000..7039e31 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +# ๐Ÿš€ Tip Calculator + +A beginner-friendly Python console app that splits a restaurant bill, including a tip, between friends. + +## ๐Ÿ“š Table of Contents + +- [Overview](#-overview) +- [Requirements](#-requirements) +- [Setup](#-setup) +- [Run](#-run) +- [Tests](#-tests) +- [License](#-license) +- [Links](#-links) + +## ๐Ÿงญ Overview + +Day 2 project from Udemy's *100 Days of Code: The Complete Python Pro Bootcamp*. The program asks for the total bill, the tip percentage and the number of people, then prints what each person should pay, rounded to two decimal places. + +``` +Welcome to the tip calculator! +What was the total bill? $124.56 +What percentage tip would you like to give? 10, 12, or 15? 12 +How many people to split the bill? 7 +Each person should pay: $19.93 +``` + +## ๐Ÿ“‹ Requirements + +- Python 3.13 or newer +- No runtime dependencies + +## ๐Ÿ› ๏ธ Setup + +Create and activate a local virtual environment: + +```powershell +# Windows (PowerShell) +python -m venv .venv +.venv\Scripts\Activate.ps1 +``` + +```bash +# macOS / Linux / Git Bash +python -m venv .venv +source .venv/bin/activate # Git Bash on Windows: source .venv/Scripts/activate +``` + +Install the project in editable mode (required to run it from `src/`; also adds the `tip-calculator` command): + +```bash +pip install -e . +``` + +Secrets such as repository tokens belong in a local `.env` file (see `.env.example`). `.env` is git-ignored; the app itself does not need it. + +## โ–ถ๏ธ Run + +```bash +python -m tip_calculator +``` + +## ๐Ÿงช Tests + +```bash +python -m unittest discover -s tests +``` + +## ๐Ÿ“„ License + +No license has been chosen yet. + +## ๐Ÿ”— Links + +- [Repository](https://git.tirsystem.com/Tirsvad-Udemy-100_days_of_code/002-tip_calculator) +- [Documentation](https://git.tirsystem.com/Tirsvad-Udemy-100_days_of_code/002-tip_calculator#readme) +- [Issue tracker](https://git.tirsystem.com/Tirsvad-Udemy-100_days_of_code/002-tip_calculator/issues) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8cb0396 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,18 @@ +[build-system] +requires = ["setuptools>=68"] +build-backend = "setuptools.build_meta" + +[project] +name = "tip-calculator" +version = "0.1.0" +description = "A beginner-friendly console app that splits a restaurant bill with a tip." +readme = "README.md" +requires-python = ">=3.13" +dependencies = [] + +[project.scripts] +tip-calculator = "tip_calculator.main:main" + +[tool.setuptools.packages.find] +where = ["src"] + diff --git a/src/tip_calculator/__init__.py b/src/tip_calculator/__init__.py new file mode 100644 index 0000000..2a233a5 --- /dev/null +++ b/src/tip_calculator/__init__.py @@ -0,0 +1 @@ +"""Tip Calculator - Day 2 of 100 Days of Code.""" diff --git a/src/tip_calculator/__main__.py b/src/tip_calculator/__main__.py new file mode 100644 index 0000000..131d401 --- /dev/null +++ b/src/tip_calculator/__main__.py @@ -0,0 +1,3 @@ +from tip_calculator.main import main + +main() diff --git a/src/tip_calculator/calculator.py b/src/tip_calculator/calculator.py new file mode 100644 index 0000000..9ed03b0 --- /dev/null +++ b/src/tip_calculator/calculator.py @@ -0,0 +1,8 @@ +def bill_per_person(bill: float, tip_percent: int, people: int) -> float: + """Return what each person pays, rounded to two decimal places.""" + if people < 1: + raise ValueError("people must be at least 1") + tip_as_percent = tip_percent / 100 + total_tip_amount = bill * tip_as_percent + total_bill = bill + total_tip_amount + return round(total_bill / people, 2) diff --git a/src/tip_calculator/main.py b/src/tip_calculator/main.py new file mode 100644 index 0000000..8722ca1 --- /dev/null +++ b/src/tip_calculator/main.py @@ -0,0 +1,14 @@ +from tip_calculator.calculator import bill_per_person + + +def main() -> None: + print("Welcome to the tip calculator!") + bill = float(input("What was the total bill? $")) + tip = int(input("What percentage tip would you like to give? 10, 12, or 15? ")) + people = int(input("How many people to split the bill? ")) + final_amount = bill_per_person(bill, tip, people) + print(f"Each person should pay: ${final_amount:.2f}") + + +if __name__ == "__main__": + main() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_calculator.py b/tests/test_calculator.py new file mode 100644 index 0000000..2e5995f --- /dev/null +++ b/tests/test_calculator.py @@ -0,0 +1,19 @@ +import unittest + +from tip_calculator.calculator import bill_per_person + + +class BillPerPersonTests(unittest.TestCase): + def test_course_example(self): + self.assertEqual(bill_per_person(124.56, 12, 7), 19.93) + + def test_tip_split_five_ways(self): + self.assertEqual(bill_per_person(150, 12, 5), 33.6) + + def test_zero_people_raises(self): + with self.assertRaises(ValueError): + bill_per_person(100, 10, 0) + + +if __name__ == "__main__": + unittest.main()