|
| 1 | +""" |
| 2 | +Simple Calculator |
| 3 | +----------------------- |
| 4 | +This program performs basic arithmetic operations: |
| 5 | +Addition, Subtraction, Multiplication, and Division. |
| 6 | + |
| 7 | +It runs in a loop so the user can perform multiple calculations |
| 8 | +until they choose to exit. |
| 9 | +""" |
| 10 | + |
| 11 | +# No extra imports needed since we're only using basic Python features |
| 12 | + |
| 13 | +def add(a, b): |
| 14 | + """Return the sum of two numbers.""" |
| 15 | + return a + b |
| 16 | + |
| 17 | +def subtract(a, b): |
| 18 | + """Return the difference between two numbers.""" |
| 19 | + return a - b |
| 20 | + |
| 21 | +def multiply(a, b): |
| 22 | + """Return the product of two numbers.""" |
| 23 | + return a * b |
| 24 | + |
| 25 | +def divide(a, b): |
| 26 | + """Return the result of dividing two numbers. |
| 27 | + Includes handling for division by zero.""" |
| 28 | + if b == 0: |
| 29 | + return "Error: Cannot divide by zero!" |
| 30 | + return a / b |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + """Main function that runs the calculator.""" |
| 35 | + print("🧮 Welcome to the Simple Calculator!") |
| 36 | + print("You can perform addition, subtraction, multiplication, and division.") |
| 37 | + print("Type 'exit' anytime to quit.\n") |
| 38 | + |
| 39 | + # Infinite loop to keep the calculator running until user exits |
| 40 | + while True: |
| 41 | + # Take user input for the first number |
| 42 | + num1 = input("Enter the first number: ") |
| 43 | + |
| 44 | + # If the user types 'exit', stop the program |
| 45 | + if num1.lower() == "exit": |
| 46 | + print("👋 Thanks for using the calculator. Goodbye!") |
| 47 | + break |
| 48 | + |
| 49 | + # Validate that input is a number |
| 50 | + try: |
| 51 | + num1 = float(num1) # Convert input to a number (supports decimals) |
| 52 | + except ValueError: |
| 53 | + print("⚠️ Invalid input! Please enter a number.") |
| 54 | + continue # Skip rest of the loop and ask again |
| 55 | + |
| 56 | + # Take input for the operator (+, -, *, /) |
| 57 | + operator = input("Enter operation (+, -, *, /): ") |
| 58 | + |
| 59 | + # Check if the operator is valid |
| 60 | + if operator not in ["+", "-", "*", "/"]: |
| 61 | + print("⚠️ Invalid operator! Please choose from +, -, *, /.") |
| 62 | + continue |
| 63 | + |
| 64 | + # Take input for the second number |
| 65 | + num2 = input("Enter the second number: ") |
| 66 | + |
| 67 | + if num2.lower() == "exit": |
| 68 | + print("👋 Thanks for using the calculator. Goodbye!") |
| 69 | + break |
| 70 | + |
| 71 | + try: |
| 72 | + num2 = float(num2) |
| 73 | + except ValueError: |
| 74 | + print("⚠️ Invalid input! Please enter a number.") |
| 75 | + continue |
| 76 | + |
| 77 | + # Perform the chosen operation |
| 78 | + if operator == "+": |
| 79 | + result = add(num1, num2) |
| 80 | + elif operator == "-": |
| 81 | + result = subtract(num1, num2) |
| 82 | + elif operator == "*": |
| 83 | + result = multiply(num1, num2) |
| 84 | + elif operator == "/": |
| 85 | + result = divide(num1, num2) |
| 86 | + |
| 87 | + # Display the result |
| 88 | + print(f"✅ Result: {num1} {operator} {num2} = {result}\n") |
| 89 | + |
| 90 | + |
| 91 | +# The entry point of the program |
| 92 | +# Ensures that main() runs only when the file is executed directly |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments