Issue
The Code is for a GUI Calculator. How do I detect the EOF parsing error from within my code?
Code:
def btnEqualsInput():
global operator
if operator!='':
sumup = str(eval(operator))
text_Input.set(sumup)
operator =""
Output when I click '=' with 3* in textbox
sumup = str(eval(operator))
File "<string>", line 1
3*
^
SyntaxError: unexpected EOF while parsing
I want to display "Error!" in the calculator display whenever user presses Equals on wrong syntax in the textbox.
Solution
Just catch the exception:
def btnEqualsInput():
global operator
if operator!='':
try:
sumup = str(eval(operator))
text_Input.set(sumup)
operator =""
except SyntaxError as e:
print("Error!",str(e)) #e contains the type of message, for example unexpected EOF while parsing
You can parse the error string as well if you want to do something specific (for EOF for example, "EOF" in str(e)
will be true)
Answered By - kabanus Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.