Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions HacktoCulator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# ---------------------------------------------------------------
# Simple Calculator Program
# Author: Your Name
# Description:
# A simple command-line calculator that performs
# addition, subtraction, multiplication, and division.
# ---------------------------------------------------------------

def add(a, b):
"""Return the sum of two numbers."""
return a + b

def subtract(a, b):
"""Return the difference between two numbers."""
return a - b

def multiply(a, b):
"""Return the product of two numbers."""
return a * b

def divide(a, b):
"""Return the quotient of two numbers."""
if b == 0:
return "Error: Division by zero is not allowed."
return a / b

def calculator():
"""Run the calculator program."""
print("Welcome to the Python Calculator!")
print("Select an operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Exit")

while True:
choice = input("\nEnter your choice (1-5): ")

if choice == '5':
print("Exiting the calculator. Goodbye!")
break

if choice in ['1', '2', '3', '4']:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
print(f"Result: {add(num1, num2)}")
elif choice == '2':
print(f"Result: {subtract(num1, num2)}")
elif choice == '3':
print(f"Result: {multiply(num1, num2)}")
elif choice == '4':
print(f"Result: {divide(num1, num2)}")

except ValueError:
print("Invalid input. Please enter numeric values.")
else:
print("Invalid choice. Please select from 1 to 5.")

# Run the program if executed directly
if __name__ == "__main__":
calculator()