I think the problem was because for special numbers it doesn't stop at brackets.

I added a clause to the while loop but the program now crashes.