davidcallanan / py-myopl-code

Interpreter for the BASIC language written in Python 3
MIT License
469 stars 326 forks source link

EAFP: Zero division check is non-pythonic #18

Closed angel-carvajal-coder closed 3 years ago

angel-carvajal-coder commented 3 years ago

The following code in the dived_by method in episodes 3-14 is non-pythonic and can be fixed Sorry if I'm rude: I'm new to GitHub:

if other.value == 0:
    return None, RTError(
        other.pos_start, other.pos_end,
        'Division by zero',
        self.context
    )

    return Number(self.value / other.value).set_context(self.context), None

to

try:
    return Number(self.value / other.value).set_context(self.context), None
except ZeroDivisionError:
   return None, RTError(
    other.pos_start, other.pos_end,
    'Division by zero',
    self.context
    )
ghost commented 3 years ago

And this is an issue because...?

angel-carvajal-coder commented 3 years ago

...Zero division check is non-pythonic!

Anyway, I realized the repository was "archived", so I'm gonna close this issue