forked from CSEdgeOfficial/Python-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
45 lines (37 loc) · 1.49 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
def calculator():
while True:
# Print options for the user
print("Enter '+' to add two numbers")
print("Enter '-' to subtract two numbers")
print("Enter '*' to multiply two numbers")
print("Enter '/' to divide two numbers")
print("Enter 'quit' to end the program")
# Get user input
user_input = input("enter your operator: ")
# Check if the user wants to quit
if user_input == "quit":
break
# Check if the user input is a valid operator
elif user_input in ["+", "-", "*", "/"]:
# Get first number
num1 = float(input("Enter a number: "))
# Get second number
num2 = float(input("Enter another number: "))
# Perform the operation based on the user input
if user_input == "+":
result = num1 + num2
print(num1, "+", num2, "=", result)
elif user_input == "-":
result = num1 - num2
print(num1, "-", num2, "=", result)
elif user_input == "*":
result = num1 * num2
print(num1, "*", num2, "=", result)
elif user_input == "/":
result = num1 / num2
print(num1, "/", num2, "=", result)
else:
# In case of invalid input
print("Invalid Input")
# Call the calculator function to start the program
calculator()