-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
25 lines (20 loc) · 900 Bytes
/
interpreter.py
File metadata and controls
25 lines (20 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# In a file called interpreter.py, implement a program that prompts the user for an arithmetic expression
# and then calculates and outputs the result as a floating-point value formatted to one decimal place.
# Assume that the user’s input will be formatted as x y z, with one space between x and y and one space between y and z, wherein:
# x is an integer
# y is +, -, *, or /
# z is an integer
# For instance, if the user inputs 1 + 1, your program should output 2.0. Assume that, if y is /, then z will not be 0.
# Note that, just as python itself is an interpreter for Python, so will your interpreter.py be an interpreter for math!
expression = str(input("Expression: ")).strip()
x, y, z = expression.split(" ")
x = int(x)
z = int(z)
if y == "+":
print(float(x + z))
elif y == "-":
print(float(x - z))
elif y == "*":
print(float(x * z))
elif y == "/":
print(float(x / z))