Skip to content

Commit ebfeefa

Browse files
authored
Add files via upload
adding a new python file not a game but a calculator
1 parent 627ed7c commit ebfeefa

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

calculator.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
def add(x,y):
2+
return x+y
3+
4+
def sub(x,y):
5+
return x-y
6+
7+
def mul(x,y):
8+
return x*y
9+
10+
def divide(x,y):
11+
if y == 0:
12+
return "Error! Division by zero ia not possible."
13+
return x/y
14+
15+
print("Select operation: ")
16+
print("1. Addition")
17+
print("2. Subtraction")
18+
print("3. Multiplication")
19+
print("4. Division")
20+
21+
while True:
22+
choice = input("Enter choice of operation (1,2,3,4): ")
23+
24+
if choice in ('1', '2', '3', '4'):
25+
try:
26+
num1 = input("Enter first number: ")
27+
num2 = input("Enter second number: ")
28+
except ValueError:
29+
print("Invalid input, Please enter a number. ")
30+
continue
31+
32+
if choice == '1':
33+
print(f"Result: {add(num1,num2)}")
34+
35+
elif choice == '2':
36+
print(f"Result: {sub(num1,num2)}")
37+
38+
elif choice == '3':
39+
print(f"Result: {mul(num1,num2)}")
40+
41+
elif choice == '4':
42+
print(f"Result: {divide(num1,num2)}")
43+
44+
else:
45+
print("Invalid choice.")
46+
47+
next_calc = input("Do you want another calculation? (yes/no): ")
48+
if next_calc.lower() != 'yes':
49+
break

0 commit comments

Comments
 (0)