How do I output the result of an expression where if the user enters a float number and an integer, the result will include decimal, and if the user gives both number in an integer form, the result will not include decimal.
Example:
num_1 = float(input("\nEnter first number: "))
opr = input("Enter math operator: ")
num_2 = float(input("Enter second number: "))
if opr == "+":
print(num_1 + num_2)
elif opr == "-":
print(num_1 - num_2)
else:
print("Invalid math operator")
>>> Enter first number: 2.5
>>> Enter math operator: +
>>> Enter second number: 3
5.5
The code above works for a float number that's because the input has been converted to float to avoid ValueError. But the result will always be a float number. How can I make 2 + 2 = 4
and not 4.0
?