Many exceptions

Report a typo

We wrote a find_sqrt() function to calculate the square root of a number and added a try-except block in case the user chooses to pass "2" (string type) instead of 2 (integer type) as an argument.

But we did not take into account that the user can call a function like this:

find_sqrt('One')

And then we get the next "long" traceback:

Traceback (most recent call last):
  File "C:/Users/laysa/PycharmProjects/untitled/file.py", line 9, in find_sqrt
    print(math.sqrt(number))
TypeError: must be real number, not str

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/laysa/PycharmProjects/untitled/file.py", line 14, in <module>
    find_sqrt('One')
  File "C:/Users/laysa/PycharmProjects/untitled/file.py", line 11, in find_sqrt
    print(math.sqrt(int(number)))
ValueError: invalid literal for int() with base 10: 'One'

Read the traceback and try to figure out where something went wrong. Correct the code so that in such situations, when the user wants to enter a number in letters, the program shows the following message instead of a traceback: Please pass a number like "5" or 5.

Tip: You can use the try-except block in the except TypeError statement.

Sample Input 1:

4

Sample Output 1:

2.0

Sample Input 2:

One

Sample Output 2:

Please pass a number like "5" or 5
Write a program in Python 3
import math

def find_sqrt(number):
try:
print(math.sqrt(number))
except TypeError:
print(math.sqrt(int(number)))
___

Create a free account to access the full topic