Skip to content

Hw2 voskoboinikov #13

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added HW2_Voskoboinikov/Heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions HW2_Voskoboinikov/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# HW2_Git_and_python

### Project Description

Данный проект содержит в себе мини-программу `calculator.py`, на вход которой через пробел подается **2 числа** типа *float* или
*integer* и одна из четырех операций (`+ - * /`), которую нужно выполнить с этими числами.

**Примеры**: `5 + 3`, `2 / 10`

### How to use

Чтобы запустить программу, необходимо в командной строке ввести `python3 calculator.py`, находясь в папке с проектом,
после чего следовать подсказкам на экране.

Чтобы выйти из программы, необходимо ввести команду `exit`.

### The Team

Александр Воскобойников - Team leader

Михаил Гринберг, Артем Васильев, Шакир Сулейманов, Гуля Мурадова - Wonderful developers team

![They are trying to make a Heart](https://github.com/ArtemVaska/HW2_Git_and_python/blob/HW2_Vasilev/HW2_Voskoboinikov/Heart.jpg)
35 changes: 35 additions & 0 deletions HW2_Voskoboinikov/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def addition(a, b):
return(a + b)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

скобки у return не нужны

Suggested change
return(a + b)
return a + b


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По PEP8 после функции делают 2 пропуска строки

def subtraction(a, b):
return(a - b)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(a - b)
return a - b


def multiplication(a, b):
return(a * b)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(a * b)
return a * b


def division(a, b):
return(a / b)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(a / b)
return a / b


def calc_parser(calc_string):
calc_list = calc_string.split(' ')
calc_list[0] = float(calc_list[0])
calc_list[2] = float(calc_list[2])
return calc_list

calculator_func_dict = {'-': subtraction,
'*': multiplication,
'+': addition,
'/': division}
Comment on lines +19 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


calc_string = input('Enter your expression: ')

while calc_string != 'exit':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здорово, что сделали возможность проводить столько вычислений, сколько нужно пользователю👍

command = calc_parser(calc_string)[1]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно один раз вызвать функцию calc_parser() и потом разом записать в переменные элементы в список, а не трижды ее вызывать. Так работает, но выглядит неоптимально.
Например:

Suggested change
command = calc_parser(calc_string)[1]
numb1, command, numb2 = calc_parser(calc_string)

if command in calculator_func_dict:
print(calculator_func_dict[command](calc_parser(calc_string)[0], calc_parser(calc_string)[2]))
else:
print('Seems like an invalid expression. Please, enter valid expression!')

calc_string = input('Enter your expression: ')

print('See you next time!')
Comment on lines +24 to +35
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот код тоже можно было обернуть в функцию

Suggested change
calc_string = input('Enter your expression: ')
while calc_string != 'exit':
command = calc_parser(calc_string)[1]
if command in calculator_func_dict:
print(calculator_func_dict[command](calc_parser(calc_string)[0], calc_parser(calc_string)[2]))
else:
print('Seems like an invalid expression. Please, enter valid expression!')
calc_string = input('Enter your expression: ')
print('See you next time!')
def main():
calc_string = input('Enter your expression: ')
while calc_string != 'exit':
command = calc_parser(calc_string)[1]
if command in calculator_func_dict:
print(calculator_func_dict[command](calc_parser(calc_string)[0], calc_parser(calc_string)[2]))
else:
print('Seems like an invalid expression. Please, enter valid expression!')
calc_string = input('Enter your expression: ')
print('See you next time!')
main()