Skip to content

Commit f6e369d

Browse files
authored
Create calculator.py
1 parent d53d448 commit f6e369d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

calculator.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Calculator
2+
def addition(x, y):
3+
return x + y
4+
5+
6+
def subtraction(x, y):
7+
return x - y
8+
9+
10+
def multiplication(x, y):
11+
return x * y
12+
13+
14+
def division(x, y):
15+
return x / y
16+
17+
18+
def main():
19+
print("""
20+
Welcome to Calculator!
21+
+
22+
-
23+
*
24+
/
25+
""")
26+
z = True
27+
while z:
28+
z = False
29+
function = input("Select your function: ")
30+
char_one = float(input("What is x? "))
31+
char_two = float(input("What is y? "))
32+
if function == "+":
33+
result = addition(x=char_one, y=char_two)
34+
elif function == "-":
35+
result = subtraction(x=char_one, y=char_two)
36+
elif function == "*":
37+
result = multiplication(x=char_one, y=char_two)
38+
elif function == "/":
39+
result = division(x=char_one, y=char_two)
40+
else:
41+
z = True
42+
print("You entered an invalid function. Try again.")
43+
print(f"{char_one} {function} {char_two} = {result}")
44+
answer = input("Would you like to do another? Y/N ")
45+
if answer.lower() == "y":
46+
finish = False
47+
else:
48+
finish = True
49+
return finish
50+
51+
52+
finished = False
53+
while not finished:
54+
finished = main()

0 commit comments

Comments
 (0)