forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
63 lines (53 loc) · 1.92 KB
/
calculator.py
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
52
53
54
55
56
57
58
59
60
61
62
63
print("Welcome to basic python calculator", "\n")
while True:
while True:
try:
num1 = int(input("Enter first digit: "))
break
except ValueError:
print("Invalid input! Please enter an integer.")
while True:
try:
num2 = int(input("Enter second digit: "))
break
except ValueError:
print("Invalid input! Please enter an integer.")
# Define operations
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def multiply(num1, num2):
return num1 * num2
def divide(num1, num2):
if num2 == 0:
return "Error! Division by zero."
return num1 / num2
# Display operation choices
print("\nChoose an operation: ")
print("Press 1 for addition (+)")
print("Press 2 for subtraction (-)")
print("Press 3 for multiplication (*)")
print("Press 4 for division (/)")
while True:
try:
choice = int(input("Enter your choice (1/2/3/4): "))
if choice == 1:
print(num1, "+", num2, "=", add(num1, num2))
elif choice == 2:
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == 3:
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == 4:
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("Invalid input! Please choose between (1-4)")
continue # Ask again if the input is not 1-4
break # Exit choice loop after a valid selection
except ValueError:
print("Invalid input! Please enter a number between 1 and 4.")
# Ask if the user wants to continue
next_calculation = input("Do you want to continue? (yes/no): ").strip().lower()
if next_calculation == "no":
print("Goodbye!")
break