So, I am currently relearning python through a series of mini projects and I am a lil bit stuck. My current issue involves a function taking in a string of numbers and operators, much like a calculator. I've set up a function that tries to do the calculation, except when a SyntaxError is produced. I want to create specific error messages for different circumstances but have no idea how to do it. My current format is of the following (say for too many decimal points):
try:
#Does the calculation
except SyntaxError:
for element in string:
if element == '.' and SyntaxError:
# Print the specified error message here
elif element == '(' or element == ')' and SyntaxError:
# Print another, but unique error message here
Currently, if the error was produced it would take the first character of the input and just return the error message associated for that first character. What I want it to do is identify what character is causing the error and then use that to print the correct error message. For now, if I input "o..45" into the program, it prints nothing as I don't have anything for a SyntaxError and a 0. I would ideally like it to instead print "You have inserted too many decimal points, please try again." I hope I explained this okay!
Aucun commentaire:
Enregistrer un commentaire