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
a334f446
Commit
a334f446
authored
4 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
move tests
parent
38c07117
master
rkalahas-master-patch-39159
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
09/arithmetic.py
+21
-5
09/arithmetic.py
tests/0.Lecture11/test_mystery.py
+0
-0
tests/0.Lecture11/test_mystery.py
with
21 additions
and
5 deletions
+21
-5
09/arithmetic.py
View file @
a334f446
...
...
@@ -10,10 +10,15 @@ def apply_operation(op: str, a: float, b: float) -> float:
return
a
/
b
elif
op
==
'*'
:
return
a
*
b
else
:
raise
NotImplementedError
(
"That operation is not implemented yet!"
)
def
as_number
(
x
:
str
)
->
float
:
return
float
(
x
)
try
:
return
float
(
x
)
except
ValueError
:
raise
TypeError
(
"not a float"
)
def
as_int
(
x
:
float
)
->
int
|
float
:
...
...
@@ -28,8 +33,8 @@ def compute_answer(expr: str) -> float:
while
''
in
pieces
:
pieces
.
remove
(
''
)
if
len
(
pieces
)
!=
3
:
print
(
"You didn't
provide t
wo operands
an
d
a
n operation
!"
)
if
len
(
pieces
)
>
3
:
raise
ValueError
(
"You
provide
d
t
oo m
an
y
a
rguments
!"
)
a
:
float
=
as_number
(
pieces
[
0
])
b
:
float
=
as_number
(
pieces
[
2
])
...
...
@@ -42,5 +47,16 @@ def compute_answer(expr: str) -> float:
while
True
:
expr
:
str
=
input
(
"Enter a mathematical expression: "
)
ans
:
float
=
compute_answer
(
expr
)
print
(
">> "
+
str
(
ans
))
try
:
ans
:
float
=
compute_answer
(
expr
)
print
(
">> "
+
str
(
ans
))
except
NotImplementedError
:
print
(
colored
(
"That's not a valid operation!"
,
"red"
))
except
IndexError
:
print
(
colored
(
"That's not enough arguments!"
,
"red"
))
except
ValueError
as
e
:
print
(
colored
(
"I only support simple math. :("
,
"blue"
))
except
TypeError
as
e
:
print
(
colored
(
"Variable are too hard for me to handle. :("
,
"yellow"
))
except
ZeroDivisionError
:
print
(
"bah."
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/0.Lecture
11/test_mystery.py
→
tests/0.Lecture11/test_mystery.py
View file @
a334f446
File moved
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