Skip to content Skip to sidebar Skip to footer

Implementing Error Message For Code In Python

def parse(expression): operators= set('*/+-') numbers= set('0123456789')#not used anywhere as of now opExtrapolate= [] numExtrapolate= [] buff=[] for i in e

Solution 1:

You can use regular expressions to do these checks:

  1. If the input contains items that are not numbers or not the allowed operators then an error message is displayed

    ifnot re.match(r'[\d.*/+\- ]+$', expression):
        print("Bad3")  # Characters exist that are not allowed

    Explanation: [\d.*/+\- ] will only match digits, your operators, and spaces, the + means to allow one or more of those characters, and the $ matches at the very end of the string. re.match() starts at the beginning of the string so this means that only those characters are allowed.

  2. If a user enters a number such as .23 or something like 554. where there is no number before the decimal place or after the decimal place then a different error is displayed.(note that a number like 0.23 is fine).

    if re.search(r'(?<!\d)\.|\.(?!\d)', expression):
        print("Bad4")  # There is a '.' without a digit before or after it

    Explanation: \. in a regex matches a literal '.' character. The | in the middle is an alternation, so the regex will match if the expression on either side of it matches. (?<!\d) means that the previous character is not a number, and (?!\d) means that the next character is not a number, so this regex means "match a '.' that is not preceeded by a digit OR match a '.' that is not followed by a digit".

  3. If the user attempts to divide by zero an error is displayed.

    if re.search(r'/ *[0.]+(?![.\d])', expression):
        print("Bad5")  # Division by 0

    Explanation: This matches / followed by any number of spaces, then one or more 0 or . characters, so this will match if anywhere in expression you have something like / 0, / 0.0, or / 0.00. The (?![.\d]) means that the next character can't be a digit or ., which will prevent you from matching something like / 0.4.

Solution 2:

I will give you some indications, but not solve it for you :). If you need more, ask a precise question and I'll answer it.

The answers I give you are NOT directly related with your code.

You can test if a string variable can be an integer by trying to cast it :

try:
    var2 = int(var)

I let you see what Error it gives

For a version that doesn't use try, you can look at the isdigit method

You can see if a string variable if one of your operator by checking it

if (var in ["+", "-", "/", "*"])

to check even more, you can look at the variable's length first

iflen(var) != and ... see above

To check if a user inputs something like .543 and refuse it, and can look at the first element of your string variable :

if myvar[0] is".":

To check if your user wants to divide by 0, you can simply check whether the last number is equals to 0

ifint(myvar)== 0:

All these expect you to be able to get operators and numbers first though.

The other solution would be to use regular expressions to perform these checks before parsing your numbers and operators. It seems quite complex compared to the exercise you are trying to achieve though as it is homework. Might be a good idea to look at them anyway.

Post a Comment for "Implementing Error Message For Code In Python"