Week 5, Python, Error Handling

Error handling in this section was explained with the help of an example to enter PI. We saw how to handle when a user enters a text instead of a float.

The user may enter 22/7 instead of 3.14. Technically the user is correct but the program will interpret it as a string. Further, it will not be able to convert this into a float.

How do we handle this?

You can split the given string into two by using split('/') and the typecast them to int, followed by a division operation.

That means we will have to do some input processing. This is not as straight forward as exception handling. Right?

Hi Aswin,

you can use below python statement and convert the result in required format.

n= eval(input(‘Enter value’))
print(n)

1 Like