Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit cf81dbe

Browse files
Add files via upload
1 parent b5be041 commit cf81dbe

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎calculator1.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This function adds two numbers
2+
def add(x, y):
3+
return x + y
4+
5+
# This function subtracts two numbers
6+
def subtract(x, y):
7+
return x - y
8+
9+
# This function multiplies two numbers
10+
def multiply(x, y):
11+
return x * y
12+
13+
# This function divides two numbers
14+
def divide(x, y):
15+
return x / y
16+
17+
18+
print("Select operation.")
19+
print("1.Add")
20+
print("2.Subtract")
21+
print("3.Multiply")
22+
print("4.Divide")
23+
24+
while True:
25+
# take input from the user
26+
choice = input("Enter choice(1/2/3/4): ")
27+
28+
# check if choice is one of the four options
29+
if choice in ('1', '2', '3', '4'):
30+
try:
31+
num1 = float(input("Enter first number: "))
32+
num2 = float(input("Enter second number: "))
33+
except ValueError:
34+
print("Invalid input. Please enter a number.")
35+
continue
36+
37+
if choice == '1':
38+
print(num1, "+", num2, "=", add(num1, num2))
39+
40+
elif choice == '2':
41+
print(num1, "-", num2, "=", subtract(num1, num2))
42+
43+
elif choice == '3':
44+
print(num1, "*", num2, "=", multiply(num1, num2))
45+
46+
elif choice == '4':
47+
print(num1, "/", num2, "=", divide(num1, num2))
48+
49+
# check if user wants another calculation
50+
# break the while loop if answer is no
51+
next_calculation = input("Let's do next calculation? (yes/no): ")
52+
if next_calculation == "no":
53+
break
54+
else:
55+
print("Invalid Input")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /