Skip to content
Advertisement

adding an assertion error for non numerical values

I am trying to add an additional assertion for values that aren’t numbers but I dont know how to go about it. I did try adding a and type(score) == float but it didn’t work, I received a value error saying a string cannot be converted to float. I just want the program to return the assertionerror message in the code when the value entered is negative and non numerical.

JavaScript

Advertisement

Answer

It looks like your user input something other than a float at the input() line. If you want this to be more resistant to things like that, you can separate out ValueError in your try/except case. ValueError will be thrown when a non-float-able value is passed to the float() function. Something like

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement