I am a beginner learning Python and trying to write a simple program for adding two numbers together. I understand how to add the two numbers, but I do not understand how to write an if
-statement that takes into account if a user tries entering something other than a number. I pasted the code below:
num_1 = float(input("Enter first number: "))
num_2 = float(input("Enter second number: "))
if num_1 and num_2 == float:
print(num_1 + num_2)
else:
print("Not a valid number")
For example, if a user enters the word dog
an error shows up and says:
ValueError: could not convert string to float: 'dog'
How do I fix this if
-statement?