-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_Basic_calculator.py
More file actions
51 lines (38 loc) · 1.16 KB
/
7_Basic_calculator.py
File metadata and controls
51 lines (38 loc) · 1.16 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
#A basic calculator program
#performs basic arithmetic operations
def addnums(x, y=0):
return f"the answer is {x + y}"
def subtract(x, y=0):
return f" the answer is {x - y}"
def multiply(x, y=0):
return f" the answer is {x * y}"
def divide(x, y=1):
return f" the answer is {x / y}"
while True:
first_int = input("input the first integer \n")
operator = input("what is the operator sign \n")
second_int = input("what is the second integer \n")
try:
first_int = int(first_int)
second_int = int(second_int)
except:
print("please input numeric digits")
continue
if operator == '+':
print(addnums(first_int, second_int))
break
elif operator == "-":
print(subtract(first_int, second_int))
break
elif operator == '*':
print(multiply(first_int, second_int))
break
elif operator == '/':
if second_int == 0 :
print("can't divide by zero")
else:
print(divide(first_int, second_int))
break
else:
print("invalid operator")
continue