Skip to content
Discussion options

You must be logged in to vote

Both of your readings are correct, and they're two separate defects — the #REVISIT should cover both:

  1. Missing parens again — s.isalpha is a bound method object, always truthy, so the else is dead code (same class of bug as atoi.py).
  2. Inverted gate — even as s.isalpha(), the logic is backwards. You want to convert when the string is numeric, so the gate should be a digit check, not an alpha check. As written (with parens added), "42" would print 0 and "x" would raise inside int(s) — the opposite of the prompt's "return 0 if invalid".

Per the prompt, the clean basic_atoi is:

def basic_atoi(s):
    if s.isdigit():
        return int(s)
    return 0

or the EAFP form (try: return int(s) excep…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dab00gieman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants