Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

David Hultén
David Hultén
4,517 Points

Im getting a AttributeError: 'NoneType' but i cant find what's wrong with the code

after running "python address_book.py" in the console i get this error message: Traceback (most recent call last):
File "/home/treehouse/workspace/address_book.py", line 32, in <module>
print(line.search(data).groupdict())
AttributeError: 'NoneType' object has no attribute 'groupdict' here is my code i used:

https://w.trhou.se/uqgfon2aut

David Hultén
David Hultén
4,517 Points

And if someone could explain how to add the code itself that would be great, bc i used a snapshot this time

1 Answer

Steven Parker
Steven Parker
243,656 Points

it looks like you're working on the Compiling and Loops lesson of the Regular Expressions in Python course.

The issue seems to be caused by several missing punctuation symbols in the REGEX expression:

# code from snapshot:
line = re.compile(r'''
    ^(?P<name>[-\w ]+,\s[-\w ]+)\t  # Last and first names
    (P<email>[-\w\d.+]+@[-\w\d.]+)\t  # Email
    (P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t  # Phone
    (P<job>[\w\s]+,\s[\w\s]+)\t  # Job and company
    (P<twitter>@[\w\d]+)?$  # Twitter
''', re.X|re.M)

# instructor's code from lesson:
line = re.compile(r'''
    ^(?P<name>[-\w ]+,\s[-\w ]+)\t  # Last and first names
    (?P<email>[-\w\d.+]+@[-\w\d.]+)\t  # Email
    (?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t  # Phone
    (?P<job>[\w\s]+,\s[\w\s.]+)\t?  # Job and company
    (?P<twitter>@[\w\d]+)?$  # Twitter
''', re.X|re.M)

This caused the search to fail and return a None, which of course has no groupdict method.

:bulb: For future questions, you can have the system provide a link to your course and lesson. Take a look at this video about Posting a Question.

:bulb: And the snapshot was a great choice as it is very helpful for analysis. But should you need to show code for other reasons (as I did here), check out this video on using Markdown formatting to preserve the code's appearance.