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.
1 Antwort
+ 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.