Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sem3-ivt19-task1-1-mozgovoy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Мозговой Никита Александрович
sem3-ivt19-task1-1-mozgovoy
Commits
97c2b194
Commit
97c2b194
authored
Nov 09, 2020
by
Мозговой Никита Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parents
Pipeline
#6379
failed with stages
in 47 seconds
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
0 deletions
+88
-0
README.md
README.md
+6
-0
calculate.py
calculate.py
+55
-0
calculate_test.py
calculate_test.py
+27
-0
No files found.
README.md
0 → 100644
View file @
97c2b194
# sem3-ivt19-task1
Простой калькулятор
## Описание задачи
Реализуйте вычиление однооперандных операций в простом калькуляторе.
Процедуры для вычисления разместите внутри функции
```calculate()```
calculate.py
0 → 100644
View file @
97c2b194
def
check_types
(
op
,
allowed_types
):
return
type
(
op
)
in
allowed_types
def
calculate
(
op1
,
op2
,
act
,
allowed_types
=
(
int
,
float
,
list
,
tuple
)):
if
not
check_types
(
op1
,
allowed_types
)
and
op1
is
not
None
:
r
=
(
"ошибка в первом операнде"
)
elif
not
check_types
(
op2
,
allowed_types
)
and
op2
is
not
None
:
r
=
(
"ошибка во втором операнде"
)
else
:
if
type
(
op1
)
!=
float
and
type
(
op2
)
!=
float
:
if
type
(
op1
)
in
(
int
,
str
):
op1
=
float
(
op1
)
if
type
(
op2
)
in
(
int
,
str
):
op2
=
float
(
op2
)
if
(
type
(
op1
)
is
list
and
type
(
op2
)
is
list
)
or
(
type
(
op1
)
is
tuple
and
type
(
op2
)
is
tuple
):
if
act
==
"+"
:
r
=
op1
+
op2
else
:
print
(
"Действие невозможно"
)
if
type
(
op1
)
in
(
list
,
tuple
)
and
op2
==
None
:
if
act
==
"+"
:
r
=
0
for
i
in
op1
:
r
+=
i
return
r
else
:
print
(
"Действие невозможно"
)
pass
if
act
==
"+"
:
r
=
op1
+
op2
elif
act
==
"-"
:
r
=
op1
-
op2
elif
act
==
"*"
:
r
=
op1
*
op2
elif
act
==
"/"
:
if
op2
!=
0
:
r
=
op1
/
op2
else
:
r
=
"деление на ноль невозможно"
else
:
r
=
"Операция не распознана"
return
r
def
main
():
operand1
=
float
(
input
(
"Enter 1 value: "
))
operand2
=
float
(
input
(
"Enter 2 value: "
))
action
=
input
(
"Enter type of operation: "
)
result
=
(
calculate
(
operand1
,
operand2
,
action
))
print
(
"Result is: "
+
str
(
result
))
if
__name__
==
"__main__"
:
main
()
print
(
"running by interpreter"
)
\ No newline at end of file
calculate_test.py
0 → 100644
View file @
97c2b194
import
calculate
def
test_sum_two_int
():
assert
calculate
.
calculate
(
1
,
2
,
"+"
)
==
3
,
"Неверная сумма"
def
test_minus_neg_n
():
assert
calculate
.
calculate
(
1
,
-
1
,
"-"
)
==
2
,
"Вычитание отрицательных чисел (по сути - сложение)"
def
test_div_by_zero
():
assert
calculate
.
calculate
(
10
,
0
,
"/"
)
==
"деление на ноль невозможно"
,
"Деление на ноль"
def
test_sum_two_float
():
assert
calculate
.
calculate
(
1.0
,
1.0
,
"+"
)
==
2.0
,
"Неверная сумма чисел с плавающей точкой"
def
test_op2_invalid_type
():
assert
calculate
.
calculate
(
1
,
"0"
,
"+"
)
==
"ошибка во втором операнде"
,
"тип операнда int или float"
def
test_op1_invalid_type
():
assert
calculate
.
calculate
(
"1"
,
0
,
"+"
)
==
"ошибка в первом операнде"
,
"тип операнда int или float"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment