This Python program reads two numbers and a character from the user.
Based on the character input, it performs one of the following operations:
+, A, a → Addition-, S, s → Subtraction*, M, m → Multiplication/, D, d → Integer division of the first number by the second"Invalid operator".
# Read inputs
num1 = float(input("Enter the first number: "))
op = input("Enter an operator (+, -, *, /, A/S/M/D or a/s/m/d): ").strip().lower()
num2 = float(input("Enter the second number: "))
# Perform operation
if op in ['+', 'a']:
result = num1 + num2
elif op in ['-', 's']:
result = num1 - num2
elif op in ['*', 'm']:
result = num1 * num2
elif op in ['/', 'd']:
if num2 != 0:
result = int(num1) // int(num2)
else:
result = "Error: Division by zero is not allowed."
else:
result = "Invalid operator"
# Display result
print("Result:", result)
Enter the first number: 10
Enter an operator: m
Enter the second number: 3
Result: 30.0
Enter the first number: 15
Enter an operator: /
Enter the second number: 4
Result: 3
- The operator is normalized using .lower() to handle both uppercase and lowercase letters
- The program checks the operator against known symbols and letters
- Integer division is performed using // after converting inputs to int
- Division by zero is handled with a conditional check
- If the operator is invalid, a message is displayed