-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
52 lines (49 loc) · 1.21 KB
/
Copy pathcalculator.py
File metadata and controls
52 lines (49 loc) · 1.21 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def calculator(num1, num2, operator):
'''
recieve input from user of two numbers and an operator and compeletes the task
Asks user for an equation, each input seperated by a space. Depending on the operator the user gives,
The function will either add(+), subtract(-), multiply(*), divide(/), integer division(//), or power(**).
If anything is invalid, it will return False
Parameter
---------
num1: int
first string inputed by user
operator: str
second string inputed by user
num2: int
third string inputed by user
Returns
-------
float
the answer to the equation
Example
-------
python3 calculator.py
Enter equation: 10 + 11
21
'''
list = ['+','-','*','/','//','**']
for i in list:
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
if num2 == 0:
return False
else:
return num1 / num2
elif operator == '//':
if num2 == 0:
return False
else:
return num1 // num2
elif operator == '**':
return num1 ** num2
else:
return False
def parse_input():
num1, operator, num2 = input ("Enter equation: ").split()
return calculator(int(num1), int(num2), operator)