0

Exception in Calculator?

https://code.sololearn.com/c5VP3ULZqqSC/?ref=app I am trying to raise an exception to print an error message when a non-float input is entered for num1 and/or num2 that would still allow the loop to continue. I tried the finally statement with no luck, maybe I am missing something somewhere. Would appreciate any help I can get, thanks in advance.

18th Sep 2018, 3:34 PM
Simon Huang
Simon Huang - avatar
1 Odpowiedź
+ 1
Using the input: add junk junk Python reports this error: 1: Traceback (most recent call last): 2: File ".\Playground\", line 43, in <module> 3: num1 = float(input('Please enter your first number: ')) 4: ValueError: could not convert string to float: 'junk' Line 2 of the error identifies the line in your code where the problem is occurring. There is no try...catch surrounding that line. Line 4 of the error identifies what went wrong: Noting that all inputs are strings, the cast to float() is throwing an error You could try adding a try...catch at that location, and similarly protect your second input.
18th Sep 2018, 5:14 PM
Kirk Schafer
Kirk Schafer - avatar