Reputation: 35
def temperature_def(tem):
try:
if tem >= 34 and tem <= 38:
return tem
else:
print("You can not enter the list!")
quit()
except:
print("Enter a number!")
quit()
pearson1 = People_class((input("Name and Surname (with out accents): ").lower()), int(input("Age: ")),
covid_status_def(input("Someone close to you either has covid or had it? ").lower().strip()),
temperature_def(float(input("What is your temperature in degrees? "))))
Here im trying to get a number for the if statement, I am taking the input in the last line.
The try and except should recognise if a number is not inputted (no idea if that is a word), and should do as it follows, but, when I type something that is not a number, it appears the error.
To clarify im doing a list and the "float(input(..." can not be moved (as far as I know).
Thanks in advance and happy holidays :D
Upvotes: 2
Views: 66
Reputation: 8564
That’s because you are explicitly trying to convert the user input to float:
temperature_def(float(input("What is your temperature in degrees? ")))
You should remove the float
from above and pass the input without doing any explicit conversion. If the input was incorrect, the try-except
defined in your method will handle it.
Edit: Since you removed the float convesion from input
, you now have to put it inside the try
block:
try:
temp = float(temp)
if tem >= 34 and tem <= 38:
return tem
else:
print("You can not enter the list!")
quit()
except:
print("Enter a number!")
quit()
Upvotes: 2