-
Notifications
You must be signed in to change notification settings - Fork 55
HW2_Islamova #3
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_Islamova #3
Changes from all commits
277be23
79c2569
a9edfd1
4fab598
728fa58
2c1c9ed
2a7632c
e1649e9
194c738
9a557fe
c2f3318
198aa93
35b870e
6827855
34cfb45
e174a8c
f7302fa
298933d
41d6062
0cc5f98
792617c
deeed66
1b79203
0491cdd
96a7a9c
1416b43
59eb89e
61057d2
e3d0d4d
33f0100
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,66 @@ | ||
| # Python Calculator | ||
|
|
||
| A simple console-based calculator implementation in Python for basic mathematical operations. | ||
| ## Features | ||
|
|
||
| - Addition (+) | ||
| - Subtraction (-) | ||
| - Multiplication (*) | ||
| - Division (/) | ||
| - Zero division error handling | ||
| - Support for negative numbers and floating-point values | ||
| - Input validation | ||
|
|
||
| ## Installation | ||
|
|
||
| Clone the repository: | ||
|
|
||
|
|
||
| git clone https://github.com/rentagr/HW2_Git_and_python.git | ||
| cd HW2_Git_and_python | ||
|
|
||
| ## Usage | ||
|
|
||
| Run the calculator: | ||
| python3 HW2_Islamova/calculator.py | ||
|
|
||
| Follow the prompts: | ||
|
|
||
| Enter a mathematical expression (example: 5 - 3): | ||
|
|
||
| The numbers and the operator must be separated by spaces. | ||
|
|
||
| Input first number: a; | ||
| Enter math operation: +, -, *, /; | ||
| Input second number: b; | ||
|
|
||
|
|
||
| ## Example | ||
|
|
||
| Enter a mathematical expression (example: 5 - 3): 15.5 * 2 | ||
| 31 | ||
|
|
||
| ## Technologies Used | ||
|
|
||
| Python 3 | ||
|
|
||
| Git version control | ||
|
|
||
| GitHub collaboration | ||
|
|
||
| ## Development Team | ||
|
|
||
| Team Members: | ||
|
|
||
| Elena Ivankina - Add function, finding errors and inaccuracies, group communication organization | ||
| Anastasia Patrusheva - Sustract function, finding errors and inaccuracies, error correction | ||
| Elena Kalita - Multiply funcrion, finding errors and inaccuracies, error correction | ||
| Mikhail Dobryakov - Division function implementation with zero division handling, finding errors and inaccuracies | ||
| Renata I. Tagirovna - Project regulation, main calculator function, input validation | ||
|
|
||
|  | ||
|
|
||
|  | ||
|
|
||
|
|
||
| ## This is an educational project developed for academic purposes |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||
| # calculator.py | ||||||||
|
|
||||||||
| AVAILABLE_OPERATORS = ['+', '-', '*', '/'] | ||||||||
|
|
||||||||
| def add(a, b): | ||||||||
| result = a + b | ||||||||
| return result | ||||||||
|
|
||||||||
|
Comment on lines
+6
to
+8
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
Не обязательно и не всегда нужно, особенно если вычисление длинное - но тут оно короткое и очевидное. Помогает читаемости кода. |
||||||||
| def subtract(a, b): | ||||||||
| result = a - b | ||||||||
| return result | ||||||||
|
|
||||||||
| def multiply(a, b): | ||||||||
| result = a * b | ||||||||
| return result | ||||||||
|
|
||||||||
| def divide(a, b): | ||||||||
| if b == 0: | ||||||||
| print("Error: Division by zero is not possible") | ||||||||
| return None | ||||||||
|
Comment on lines
+18
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. Классно, что об этом подумали, и еще более здорово, какую выбрали реализацию. Но вообще говоря, тут будет ошибка деления на ноль - и ладно. Но об ошибках и работе с ними еще будет. Клево, что возвращаете именно None, очень правильное направление мысли. |
||||||||
| result = a / b | ||||||||
| return result | ||||||||
|
|
||||||||
| def main (): | ||||||||
| while True: | ||||||||
| # Getting the input expression from the user | ||||||||
| expression = input("Enter a mathematical expression (example: 5 - 3): ") | ||||||||
| if not expression: | ||||||||
| print("Error: empty input") | ||||||||
| continue | ||||||||
| # Split the string into parts by spaces | ||||||||
| parts = expression.split() | ||||||||
|
|
||||||||
| # Checking the input format | ||||||||
| if len(parts) != 3: | ||||||||
| print("Error: Incorrect input format") | ||||||||
| continue | ||||||||
|
|
||||||||
| # Checking whether the operands are numbers | ||||||||
| try: | ||||||||
| a = float(parts[0]) | ||||||||
| b = float(parts[2]) | ||||||||
| except ValueError: | ||||||||
| print("Error: the numbers must be numeric or float types") | ||||||||
| continue | ||||||||
|
|
||||||||
| # Checking the operand | ||||||||
| operator = parts[1] | ||||||||
| if operator not in AVAILABLE_OPERATORS: | ||||||||
| print(f"Error: unsupported operator '{operator}'. Available operators: {AVAILABLE_OPERATORS}") | ||||||||
| continue | ||||||||
|
|
||||||||
| # Select the appropriate function to calculate | ||||||||
| if operator == '+': | ||||||||
| result = add(a, b) | ||||||||
| elif operator == '-': | ||||||||
| result = subtract(a, b) | ||||||||
| elif operator == '*': | ||||||||
| result = multiply(a, b) | ||||||||
| elif operator == '/': | ||||||||
| result = divide(a, b) | ||||||||
|
|
||||||||
| # Output of the result if it is not None | ||||||||
| if result is not None: | ||||||||
| print(result) | ||||||||
| return | ||||||||
| else: | ||||||||
| continue | ||||||||
|
|
||||||||
|
|
||||||||
| # Start the program | ||||||||
| 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.
Лучше, конечно, избегать названий в один символ, если это не счетчики.
num1, num2 оптимальнее.