Back to: Python Programming
0
This exercise is to practise mathematical operations and conditionals.
Write some code to ask a user for a number, then an operator, then another number. Depending on the user’s input, we want to perform the calculation and provide the answer (rounded to 3 decimal places).
Solution:
num1 = float(input("Enter the 1st number: "))
operator = input("Enter an operator (+ - * /): ")
num2 = float(input("Enter the 2nd number: "))
if operator == "+":
print(num1 + num2)
elif operator == "-":
print(num2 - num1)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
print(round(num1 / num2, 3))
else:
print(f"{operator} is not a valid operator")