create a calculator with user input in python

Here's how you can create a simple calculator in Python that takes user input and performs arithmetic operations based on the user's choice:

main.py
# Take user input for two numbers and the operation they want to perform
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")

# Perform the appropriate arithmetic operation based on the operator
if operator == '+':
    result = num1 + num2
elif operator == '-':
    result = num1 - num2
elif operator == '*':
    result = num1 * num2
elif operator == '/':
    result = num1 / num2
else:
    # Handle invalid operator input
    print("Invalid operator input, please enter a valid operator.")

# Print the result of the arithmetic operation
print(f"{num1} {operator} {num2} = {result}")
672 chars
21 lines

This code takes user input for two numbers and the arithmetic operation they want to perform. Then, it uses conditional statements (if-elif-else) to determine which arithmetic operation to perform based on the operator input. Finally, it prints the result of the arithmetic operation.

gistlibby LogSnag