-
Notifications
You must be signed in to change notification settings - Fork 56
Hw2 Yury Popov #14
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 Yury Popov #14
Changes from all commits
1db3889
1aac352
53c1b1c
3dc5449
a53ac44
26253b3
4ab4bc0
f5f3141
d09d378
6ca167e
17388bd
b439fa0
f10681d
fb658b4
b974593
7f746a1
336befb
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,42 @@ | ||
# Calculator | ||
|
||
This readme describes mini-program Calculator you can find here. | ||
|
||
Calculator can calculate easy arythmetical functions: | ||
* Addition | ||
* Subtraction | ||
* Multiplication | ||
* Division | ||
|
||
## How it works | ||
|
||
1. Clone this repo using SSH or HTTPS: | ||
|
||
```bash | ||
git clone [email protected]:YuryPopov/HW2_Git_and_python.git | ||
``` | ||
**or** | ||
```bash | ||
git clone https://github.com/YuryPopov/HW2_Git_and_python.git | ||
``` | ||
2. Launch application (I hope, that you are inside app folder): | ||
```bash | ||
python3 ./calculator.py | ||
``` | ||
3. After prompt (*Enter expression:*) enter your expression (numbers and arythmetical operator divided with spaces) and press *"Enter"* button. | ||
***Pay attention***, programm works with integrer and float numbers and only with +, -, *, / operators. | ||
**Enter expression like | ||
``` | ||
1 + 3 | ||
``` | ||
4. Enjoy the results. | ||
|
||
|
||
## Your developers: | ||
* Yury Popov | ||
* Kristina Zhur | ||
* Maria Uzun | ||
* Anastasia Shtompel | ||
* Alina Potyseva | ||
|
||
 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
def substract(a, b): | ||
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 a - b | ||
|
||
|
||
def divide(a, b): | ||
if b == 0: | ||
return "Error: division by zero" | ||
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. Идея неплохая, но 2 момента:
|
||
return a / b | ||
|
||
|
||
def multiplication(a, b): | ||
answer = a * b | ||
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. nit: можно сразу |
||
return answer | ||
|
||
|
||
def summing(a, b): | ||
return a + b | ||
|
||
|
||
list_of_operators = ['/', '*', '-', '+'] | ||
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. Если вы пишете это за пределами функции, тогда тут это константа :) |
||
|
||
|
||
def main(keyboard_input: str) -> float: | ||
''' | ||
Function checks operators, transform numbers to float, | ||
call math function described above (substract, divide, | ||
multiplication, summing) and return result. If operators | ||
are not in list or numbers cannot transform into floats, | ||
func returns error message. | ||
''' | ||
math_sign, operator_position = False, False | ||
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. Почему |
||
|
||
for char in range(len(keyboard_input)): | ||
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. тут явно не А вообще можно было бы вот так: for i, char in enumerate(keyboard_input):
if char in list_of_operators:
math_sign = char
operator_position = i
break |
||
if keyboard_input[char] in list_of_operators: | ||
math_sign = keyboard_input[char] | ||
operator_position = char | ||
|
||
if math_sign == False: | ||
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. Зачем? ))) if not math_sign: |
||
return 'Enter valid expression' | ||
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. Опять же по идее должны делать |
||
|
||
num1 = keyboard_input[:operator_position].strip() | ||
num2 = keyboard_input[operator_position + 1:].strip() | ||
|
||
try: | ||
num1 = float(num1) | ||
num2 = float(num2) | ||
if math_sign == '/': | ||
return divide(num1, num2) | ||
|
||
elif math_sign == '-': | ||
return substract(num1, num2) | ||
|
||
elif math_sign == '+': | ||
return summing(num1, num2) | ||
|
||
elif math_sign == '*': | ||
return multiplication(num1, num2) | ||
|
||
except ValueError: | ||
return 'Enter valid expression' | ||
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. Та же история с |
||
|
||
|
||
if __name__ == '__main__': | ||
print(main(input('Enter expression:'))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ZoneTransfer] | ||
ZoneId=3 | ||
HostUrl=https://web.telegram.org/ | ||
Comment on lines
+1
to
+3
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. Это зачем тут? :) |
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.
В следующий раз сразу меняй содержимое
README.md
:)