Repository navigation
basicatoi.py — is the #REVISIT about the missing parens or the isalpha choice?
#11
|
def basic_atoi(s):
if s.isalpha:
satoi = int(s)
else:
satoi = 0
print(satoi)
basic_atoi("42")
# basic_atoi("x")
#REVISITTwo things look off: |
Replies: 1 comment
|
Both of your readings are correct, and they're two separate defects — the
Per the prompt, the clean def basic_atoi(s):
if s.isdigit():
return int(s)
return 0or the EAFP form ( |
Both of your readings are correct, and they're two separate defects — the
#REVISITshould cover both:s.isalphais a bound method object, always truthy, so theelseis dead code (same class of bug asatoi.py).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 print0and"x"would raise insideint(s)— the opposite of the prompt's "return 0 if invalid".Per the prompt, the clean
basic_atoiis:or the EAFP form (
try: return int(s) excep…