Skip to content

Commit 9e3a2a9

Browse files
committed
Improved existing calculator
1 parent f3dabfe commit 9e3a2a9

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

calculator.py

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1-
num1=int(input("eneter a digit"))
2-
num2=int(input("eneter a another digit"))
3-
# defination for operators
4-
5-
#addition
6-
def add(num1, num2):
7-
return num1+num2
8-
#substraction
9-
def subtract(num1, num2):
10-
return num1-num2
11-
#multiply
12-
def multiply(num1, num2):
13-
return num1*num2
14-
#division
15-
def divide(num1, num2):
16-
return num1/num2
17-
18-
#command for operation
19-
print("choose operation")
20-
print("press 1 for add")
21-
print("press 2 for subs")
22-
print("press 3 for multiply")
23-
print("press 4 for devision")
24-
25-
26-
1+
while True:
2+
# definition for operators
3+
4+
#addition
5+
def add(num1, num2):
6+
return num1+num2
7+
8+
#substraction
9+
def subtract(num1, num2):
10+
return num1-num2
11+
12+
#multiplication
13+
def multiply(num1, num2):
14+
return num1*num2
15+
16+
#division
17+
def divide(num1, num2):
18+
try:
19+
return num1/num2
20+
except ZeroDivisionError:
21+
print ("WARNING: Invalid division, cannot divide by zero")
22+
23+
#exponent
24+
def exponent(num1, num2):
25+
return num1**num2
26+
27+
while True:
28+
try:
29+
num1=float(input("Enter a digit: "))
30+
num2=float(input("Enter another digit: "))
31+
break
32+
except ValueError:
33+
print("The input was not a valid digit")
2734

35+
#command for operation
36+
print("Choose an operation")
37+
print("Press 1 for addition")
38+
print("Press 2 for substraction")
39+
print("Press 3 for multiplication")
40+
print("Press 4 for division")
41+
print("Press 5 for exponent")
2842

29-
while True:
3043
# take input from the user
31-
choice = input("Enter choice(1/2/3/4): ")
44+
choice = input("Enter choice(1/2/3/4/5): ")
3245

33-
if choice in ('1', '2', '3', '4'):
46+
if choice in ('1', '2', '3', '4', '5'):
3447

3548
if choice == '1':
3649
print(num1, "+", num2, "=", add(num1, num2))
3750

38-
39-
4051
elif choice == '2':
4152
print(num1, "-", num2, "=", subtract(num1, num2))
4253

4354
elif choice == '3':
4455
print(num1, "*", num2, "=", multiply(num1, num2))
4556

46-
47-
48-
49-
5057
elif choice == '4':
5158
print(num1, "/", num2, "=", divide(num1, num2))
59+
60+
elif choice == '5':
61+
print(num1, "to the power of", num2, "=", exponent(num1, num2))
62+
5263
# check if user wants another calculation
5364
# break the while loop if answer is no
54-
next_calculation = input("Let's do next calculation? (yes/no): ")
65+
next_calculation = input("Do you want to do another calculation? (yes/no): ")
5566
if next_calculation == "no":
5667
break
5768

5869
else:
59-
print("Invalid Input")
70+
print("Invalid input")

0 commit comments

Comments
 (0)