1

I'm currently working through Automate the Boring Stuff with Python book and run into curious issue on chapter 7. When trying to execute the following code:

def isPhoneNumber(text):
 if len(text) != 12:
 return False
 for i in range(0, 3):
 if not text[i].isdecimal():
 return False
 if text[3] != "-":
 return False
 for i in range(4, 7):
 if not text(i).isdecimal():
 return False
 if text[7] != "-":
 return False
 for i in range(8, 12):
 if not text[i].isdecimal():
 return False
 return True
print("415-555-4242 is a phone number:")
print(isPhoneNumber("415-555-4242"))
print("Moshi moshi is a phone number:")
print(isPhoneNumber("Moshi moshi"))

I get the following error message:

Traceback (most recent call last):
 File "automation.py", line 27, in <module>
 print(isPhoneNumber("415-555-4242"))
 File "automation.py", line 13, in isPhoneNumber
 if not text(i).isdecimal():
TypeError: 'str' object is not callable

Switching the str.isdecimal() method for str.isdigit() method allows me to execute the code properly, but I would like to know why the isdecimal() method will not work?

asked Jan 1, 2017 at 22:54
6
  • 1
    If your book teaches you this sort of Python, then you'd better find another one. The code is very unpythonic. Commented Jan 1, 2017 at 22:58
  • 1
    As @EliKorvigo says, this would be an excellent use case for regular expressions. The regular expression '\d{3}-\d{3}-\d{4}' matches your kind of phone numbers. Commented Jan 1, 2017 at 23:08
  • text(i).isdigit() would raise the same error. You must have fixed the () typo at the same time. Commented Jan 1, 2017 at 23:54
  • @EliKorvigo: Do not blame the book. All CS1-level books in Python follow this approach. Regular expressions are extremely hard to grasp for those who are taking their first programming course. Commented Jan 2, 2017 at 0:02
  • @DYZ I'm more concerned about this iteration style. Commented Jan 2, 2017 at 8:29

2 Answers 2

6

The error has nothing to do with isdecimal(). You have a typo in how you extract the character from text. The

if not text(i).isdecimal():

should read

if not text[i].isdecimal():

(note the square brackets.)

answered Jan 1, 2017 at 22:56
Sign up to request clarification or add additional context in comments.

2 Comments

I am not the downvoter, but perhaps, guessing, he thought that this answer should instead have been a comment, coupled with a vote to close as off-topic because just a typo.
@NPE Thank you! Should have paid more attention for possible typos.
1
 File "automation.py", line 13, in isPhoneNumber
 if not text(i).isdecimal():
TypeError: 'str' object is not callable

The typeError is on line 13, where you are calling a string object. Use [] not ()

TrakJohnson
2,2052 gold badges23 silver badges36 bronze badges
answered Jan 1, 2017 at 23:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.