-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcalc.py
More file actions
21 lines (18 loc) · 775 Bytes
/
calc.py
File metadata and controls
21 lines (18 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# functions in Python
global operand1, operand2 # global variables for use in more than 1 function
# declare function with "def", name function with add, set 2 parameters to the add function
def add(operand1, operand2):
sum = operand1 + operand2 # sum is a local variable
return sum
def subtract(operand1, operand2):
diff = operand1 - operand2
return diff
option = int(input("Enter an option\n1. Add\n2. Subtract\n"))
if option == 1:
op1 = int(input("Enter the first operand: "))
op2 = int(input("Enter the second operand: "))
print(add(op1, op2)) # print the result of the add function by calling it (sum)
else:
op1 = int(input("Enter the first operand: "))
op2 = int(input("Enter the second operand: "))
print(subtract(op1, op2))