Skip to content

Commit 9df8647

Browse files
Create HacktoCulator.py
1 parent 55ea0cc commit 9df8647

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

HacktoCulator.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ---------------------------------------------------------------
2+
# Simple Calculator Program
3+
# Author: Your Name
4+
# Description:
5+
# A simple command-line calculator that performs
6+
# addition, subtraction, multiplication, and division.
7+
# ---------------------------------------------------------------
8+
9+
def add(a, b):
10+
"""Return the sum of two numbers."""
11+
return a + b
12+
13+
def subtract(a, b):
14+
"""Return the difference between two numbers."""
15+
return a - b
16+
17+
def multiply(a, b):
18+
"""Return the product of two numbers."""
19+
return a * b
20+
21+
def divide(a, b):
22+
"""Return the quotient of two numbers."""
23+
if b == 0:
24+
return "Error: Division by zero is not allowed."
25+
return a / b
26+
27+
def calculator():
28+
"""Run the calculator program."""
29+
print("Welcome to the Python Calculator!")
30+
print("Select an operation:")
31+
print("1. Addition (+)")
32+
print("2. Subtraction (-)")
33+
print("3. Multiplication (*)")
34+
print("4. Division (/)")
35+
print("5. Exit")
36+
37+
while True:
38+
choice = input("\nEnter your choice (1-5): ")
39+
40+
if choice == '5':
41+
print("Exiting the calculator. Goodbye!")
42+
break
43+
44+
if choice in ['1', '2', '3', '4']:
45+
try:
46+
num1 = float(input("Enter first number: "))
47+
num2 = float(input("Enter second number: "))
48+
49+
if choice == '1':
50+
print(f"Result: {add(num1, num2)}")
51+
elif choice == '2':
52+
print(f"Result: {subtract(num1, num2)}")
53+
elif choice == '3':
54+
print(f"Result: {multiply(num1, num2)}")
55+
elif choice == '4':
56+
print(f"Result: {divide(num1, num2)}")
57+
58+
except ValueError:
59+
print("Invalid input. Please enter numeric values.")
60+
else:
61+
print("Invalid choice. Please select from 1 to 5.")
62+
63+
# Run the program if executed directly
64+
if __name__ == "__main__":
65+
calculator()

0 commit comments

Comments
 (0)