-
Notifications
You must be signed in to change notification settings - Fork 55
HW2_Laskina #16
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
base: main
Are you sure you want to change the base?
HW2_Laskina #16
Changes from all commits
02406bb
ffa51d2
4e9573e
15a8850
c2f804d
6b2c2b0
863b1fd
93e9051
d1def60
055e61d
569cf09
3db7e13
fa30482
62f30a2
62b6eb5
a5fdb13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # 🧮 HW2 Team Calculator | ||
|
|
||
| > A simple Python calculator that evaluates mathematical expressions in the format: `number operator number`. | ||
|
|
||
| --- | ||
|
|
||
| ## 📌 Description | ||
|
|
||
| This repository contains a team-developed implementation of a basic calculator as part of a homework assignment. | ||
| The program accepts an expression via `input()`, for example: | ||
|
|
||
| `5 + 3` | ||
|
|
||
| and prints the result of the calculation. | ||
|
|
||
| Supported operations: `+`, `-`, `*`, `/`. | ||
|
|
||
| Each mathematical operation is implemented in a separate function: | ||
| - `add(x, y)` — addition | ||
| - `sub(x, y)` — subtraction | ||
| - `mul(x, y)` — multiplication | ||
| - `div(x, y)` — division (with error handling for division by zero) | ||
|
|
||
| The main logic for parsing and routing the expression is handled in the `main()` function. | ||
|
|
||
| ## ⚠️ Input Errors | ||
|
|
||
| The calculator validates your input and will show error messages in the following cases: | ||
|
|
||
| - Missing spaces: | ||
| `5+3` → Error: the expression must be in the format `<number> <operator> <number>` | ||
|
|
||
| - Comma as decimal separator: | ||
| `3,5 + 2` → Error: use a dot for the decimal part, not a comma. | ||
|
|
||
| - Unsupported operator: | ||
| `5 ^ 2` → Error: operator '^' is not supported. Available: +, -, *, / | ||
|
|
||
| - Division by zero: | ||
| `7 / 0` → Error: Division by zero is not allowed | ||
|
|
||
| --- | ||
|
|
||
| ## 👥 Development Team | ||
|
|
||
| | Contributor | Contribution | | ||
| |---------------------|-------------------------------------------| | ||
| | Natalia Laskina | `main()` — main logic | | ||
| | Daria Bobrova | `add()` — addition | | ||
| | Vadim Bolshakov | `sub()` — subtraction | | ||
| | Nikita Maksimov | `mul()` — multiplication | | ||
| | Artem Stetoi | `div()` — division, `main()`, `parse_number()` | | ||
|
|
||
| --- | ||
|
|
||
| ## 🚀 How to Use | ||
|
|
||
| ### Requirements: | ||
| - Python 3.6 or higher | ||
|
|
||
| ### Running the Program: | ||
|
|
||
| 1. Clone the repository (if not already done): | ||
| ```bash | ||
| git clone -b HW2_Laskina https://github.com/NataliaLaskina/HW2_NBLaskina.git | ||
|
|
||
| 2. Navigate to the project folder: | ||
| ```bash | ||
| cd HW2_NBLaskina | ||
| 3. Run the calculator: | ||
| ```bash | ||
| python HW2_Laskina/calculator.py | ||
| 4. Enter an expression in the format: | ||
| ```bash | ||
| <number> <operator> <number> | ||
|
|
||
| #Example: 10 - 4 | ||
| 5. The program will output the result: | ||
| 6 | ||
|
|
||
| 📂 Project Structure | ||
|
|
||
| - `HW2_NBLaskina/` — root folder | ||
| - `HW2_Laskina/` — main project folder | ||
| - `calculator.py` — main script with 6 functions: | ||
| - `add(x, y)` | ||
| - `sub(x, y)` | ||
| - `mul(x, y)` | ||
| - `div(x, y)` | ||
| - `parse_number(token)` | ||
| - `main()` | ||
| - `imgs/` — folder for project screenshots | ||
| - `README.md` — this file | ||
|
|
||
| ✨ Completed as part of a course assignment. All team members contributed equally to the development. | ||
|
|
||
|  |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||
| correct_oper = ["+", "-", "*", "/"] | ||||||||||||
| def div(x: float, y: float) -> float: | ||||||||||||
| if y == 0: | ||||||||||||
| raise ZeroDivisionError("Division by zero is not allowed") | ||||||||||||
| return x / y | ||||||||||||
|
|
||||||||||||
| def add(x: float, y: float) -> float: | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
| return x + y | ||||||||||||
|
|
||||||||||||
| def mul(x: float, y: float) -> float: | ||||||||||||
| return x * y | ||||||||||||
|
|
||||||||||||
| def sub(x: float, y: float) -> float: | ||||||||||||
| return x - y | ||||||||||||
|
|
||||||||||||
| def parse_number(token: str): | ||||||||||||
| if "." in token or "e" in token.lower(): | ||||||||||||
| return float(token) | ||||||||||||
| return int(token) | ||||||||||||
| def main(): | ||||||||||||
|
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Не хватает отступов в паре мест. Это очень помогает читаемости, честное слово. |
||||||||||||
| while True: | ||||||||||||
| calc = input("Enter an expression like: <number> <operator> <number>\n" | ||||||||||||
| "Example: 2.5 * 4 \n> ").strip() | ||||||||||||
|
|
||||||||||||
| if "," in calc: | ||||||||||||
| print("Error: use a dot for the decimal part, not a comma.") | ||||||||||||
| continue | ||||||||||||
|
|
||||||||||||
| parts = calc.split() | ||||||||||||
| if len(parts) != 3: | ||||||||||||
| print("Error: the expression must be in the format: <number> <operator> <number>") | ||||||||||||
| continue | ||||||||||||
|
|
||||||||||||
| left, op, right = parts | ||||||||||||
|
|
||||||||||||
| if op not in correct_oper: | ||||||||||||
| print(f"Error: operator '{op}' is not supported. Available: {', '.join(correct_oper)}") | ||||||||||||
| continue | ||||||||||||
|
|
||||||||||||
| try: | ||||||||||||
| x = parse_number(left) | ||||||||||||
| y = parse_number(right) | ||||||||||||
| except ValueError: | ||||||||||||
| print("Error: invalid number. Use integers or real values.") | ||||||||||||
| continue | ||||||||||||
|
|
||||||||||||
| try: | ||||||||||||
| if op == "+": | ||||||||||||
| res = add(x, y) | ||||||||||||
| elif op == "-": | ||||||||||||
| res = sub(x, y) | ||||||||||||
| elif op == "*": | ||||||||||||
| res = mul(x, y) | ||||||||||||
| elif op == "/": | ||||||||||||
| res = div(x, y) | ||||||||||||
| except ZeroDivisionError as e: | ||||||||||||
| print(f"Error: {e}") | ||||||||||||
| continue | ||||||||||||
|
|
||||||||||||
| print("Result:", res) | ||||||||||||
|
|
||||||||||||
| if __name__ == "__main__": | ||||||||||||
| main() | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Огонь, что сделана ошибка - но тогда пусть бы уже само кидало деление на ноль?