This script takes two numerical inputs from the user and an operator (+, -, *, /) to perform a calculation. It then displays the result in a clear format.
Practice creating Python programs to perform arithmetic and string, experimenting with variables, and exploring data types.
Accepts floating-point and integer inputs
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
python calculator.py
Enter first number: 10
Enter second number: 5
Choose operator (+,-,*,/): *
10.0 * 5.0 = 50.0
first_num = float(input("Enter first number: "))
second_num = float(input("Enter second number: "))
operator = input("Choose operator (+,-,*,/)")
if(operator == '+'):
result = first_num + second_num
elif(operator == '-'):
result = first_num - second_num
elif(operator == '*'):
result = first_num * second_num
elif(operator == '/'):
result = first_num / second_num
else:
print("operator not recognize")
print(f"{first_num} {operator} {second_num} = {result}")
`
No error handling for division by zero is currently implemented. If an invalid operator is entered, the script will notify the user but may still attempt to print the result, which could raise an error.