Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Can you write code in Python for making a simple calculator that adds and subtracts?

  1. User

    Can you write code in Python for making a simple calculator that adds and subtracts?

  2. Assistant

    Certainly! Here's an example of Python code for a simple calculator that can add and subtract numbers:

    def add(x, y):
        return x + y
    
    def subtract(x, y):
        return x - y
    
    def calculator():
        print("Select operation:")
        print("1. Add")
        print("2. Subtract")
    
        choice = input("Enter your choice (1 or 2): ")
    
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
    
        if choice == '1':
            result = add(num1, num2)
            operation = '+'
        elif choice == '2':
            result = subtract(num1, num2)
            operation = '-'
        else:
            print("Invalid choice!")
            return
    
        print(f"\n{num1} {operation} {num2} = {result}")
    
    
    calculator()
    

    Just copy and paste this code into your Python environment or file, run it, and you should be able to perform basic addition and subtraction calculations.