Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I do handle a error in a error handling code?

I am trying to do this code:

try:
    print(int(number1)*int(number2))
except ValueError:
    print(int(number1)*float(number2))
except ValueError:
    print(float(number1)*float(number1))
except ValueError:
   print(float(number1)*int(number1))

But it finds an error before finishing the code.
Can anyone help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I will assume you need nested try / except:

number1 = '123'
number2 = '456.78'
try:
    print(int(number1)*int(number2))
except ValueError:
    try:
        print(int(number1)*float(number2))
    except ValueError:
        try:
            print(float(number1)*float(number1))
        except ValueError:
            print(float(number1)*int(number1))

Prints:

56183.939999999995
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading