Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
?
lecture-code
Commits
6bfd5195
Commit
6bfd5195
authored
4 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
autocommit
parent
0605c2e2
master
rkalahas-master-patch-39159
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
09/arithmetic.py
+46
-0
09/arithmetic.py
with
46 additions
and
0 deletions
+46
-0
09/arithmetic.py
0 → 100644
View file @
6bfd5195
from
termcolor
import
colored
def
apply_operation
(
op
:
str
,
a
:
float
,
b
:
float
)
->
float
:
if
op
==
'+'
:
return
a
+
b
elif
op
==
'-'
:
return
a
-
b
elif
op
==
'/'
:
return
a
/
b
elif
op
==
'*'
:
return
a
*
b
def
as_number
(
x
:
str
)
->
float
:
return
float
(
x
)
def
as_int
(
x
:
float
)
->
int
|
float
:
if
int
(
x
)
==
x
:
return
int
(
x
)
else
:
return
x
def
compute_answer
(
expr
:
str
)
->
float
:
pieces
:
list
[
str
]
=
expr
.
split
(
" "
)
while
''
in
pieces
:
pieces
.
remove
(
''
)
if
len
(
pieces
)
!=
3
:
print
(
"You didn't provide two operands and an operation!"
)
a
:
float
=
as_number
(
pieces
[
0
])
b
:
float
=
as_number
(
pieces
[
2
])
op
:
str
=
pieces
[
1
]
answer
:
float
=
apply_operation
(
op
,
a
,
b
)
return
as_int
(
answer
)
while
True
:
expr
:
str
=
input
(
"Enter a mathematical expression: "
)
ans
:
float
=
compute_answer
(
expr
)
print
(
">> "
+
str
(
ans
))
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help