-
Notifications
You must be signed in to change notification settings - Fork 56
Hw2 tuliavko #11
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 tuliavko #11
Conversation
Add subtraction function, add README.md and update main function
Udpdate README.md and update main function from calculator.py
Hw2 tuliavko
Add division function
Add addition function
В целом отлично! Коммиты названы хорошо. Отдельное спасибо за PEP8! |
if num2 == 0: | ||
print('Error: division by zero! :(') | ||
else: | ||
return num1 / num2 |
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.
В целом идея неплохая, но 2 момента:
- Лучше уж тогда не print, потому что код не остановится. Вы же пишете эту функцию, чтоб получить результат деления. А в случае 0 вам вернется None и дальше где-нибудь программа упадет. Так что конкретно в этом случае можно либо
return
(но тоже плохо, т.к. мы ожидаем получить число), либо (если прям хочется перезаписать ошибку) делатьraise
. Но об этом мы поговорим во 2-ом семестре :) - В целом-то можно даже и не обрабатывать :)
Ну то есть вот пользователь ввел 0 --> программа упала сZeroDivisionError
. Ожидаемое поведение :)
У нас тут нет какого-то варианта, чтоб при 0 мы бы все-равно хотели что-то да рассчитать.
return num1 / num2 | ||
|
||
def main(): | ||
task = input('Enter your expression:') |
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.
task
? ))
Чем не понравилось название expression
, например ))
|
||
def main(): | ||
task = input('Enter your expression:') | ||
task = task.split(" ") |
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.
В целом split
по умолчанию разбивает по пробелам, так что можно просто .split()
task = input('Enter your expression:') | ||
task = task.split(" ") | ||
num1, num2 = float(task[0]), float(task[2]) | ||
mode = task[1] |
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.
имхо, вместо mode
-- operator
# dictionary with functions names | ||
operations = {'/': division, | ||
'-': subtraction, | ||
'+': addition, | ||
'*': multiplication | ||
} |
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.
Строго говоря, эта штука является константой, поэтому имеет смысл назвать её прописными буквами:
operations
--> OPERATIONS
result = main() | ||
print(result) |
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.
Тут бы топ было бы сделать вот так:
if __name__ == '__main__':
result = main()
print(result)
|
||
### Run calculator: | ||
|
||
Execute `extract_interface_residues.py` script provided here to start calculations. Then enter your expression in the line below in following format: the first number, the operation sign + for sum, - for subsctraction, / for division or \* for multiplication and the second number. Note that numbers must be separated by a space from the sign!! To run calculations press the `Enter` key. |
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.
Круто, вот только почему extract_interface_residues.py
? Файл же называется calculator.py
:)
# input expressions | ||
Enter your expression:1 + 2 | ||
3 |
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.
Вот это тут (в блоке с косыми кавычками) немного лишнее. Обычно в таком блоке пишется код/скрипты и прочее, что через интерфейс можно скопировать и вставить.
This is the repo for the second homework of the BI Python 2023 course:
Badmadashiev Dorzhi
Vedekhina Veronika
Tuliavko Vlada
Matveeva Ksenia
Vaganova Polina