gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
can you give me a simple python code for a calculator
- User
can you give me a simple python code for a calculator
- Assistant
Sure, here is a simple Python code for a calculator with basic arithmetic operation (+, -, *, /):
# Simple Calculator in Python # Function to Addition def add(x, y): return x + y # Function to Subtraction def sub(x, y): return x - y # Function to Multiplication def mul(x, y): return x * y # Function to Division def div(x, y): return x / y # Take input from the user print("Select an operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") # Take user choice choice = input("Enter choice(1/2/3/4): ") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) # Condition for the calculation if choice == '1': print(num1, "+", num2, "=", add(num1,num2)) elif choice == '2': print(num1, "-", num2, "=", sub(num1,num2)) elif choice == '3': print(num1, "*", num2, "=", mul(num1,num2)) elif choice == '4': print(num1, "/", num2, "=", div(num1,num2)) else: print("Invalid input")This code takes input from the user for the operation choice (addition, subtraction, multiplication or division) and performs the corresponding calculation on two numbers entered by the user.