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 Regular Expressions in Python Introduction to Regular Expressions Escapes

Is there a way to check my work against the answer key in the challenges?

I have something wrong in my code below and it would be nice to be able to check against the answer key so I can learn from it if I cannot figure it out.

import re
def first_number(string):
    re.search(r'\d', string)
    return 'string'

2 Answers

Daniel Medina
Daniel Medina
13,863 Points

You are very close with what you have.

Right now you are returning 'string.' You'll need to move your return statement so that you're returning what you're searching for in your regex.

Regarding your question on answer keys:

I think a great workaround would be to open the Python shell in Treehouse workspaces and type your code out in there. By print()'ing out your code in certain areas, you can double check what your input is with what's being expected by the challenge. This gives you immediate feedback without the need for any external software.

Another solution is to type the code out locally in your favorite IDE (e.g. VS Code, PyCharm Community Edition, Thonny), set up breakpoints, and step through the code. Observe how variables and their values change. See which, if any, conditions are affecting them.

This process can be daunting especially if it is new material, but once you get started with it, it is a powerful way to debug your code and pinpoint errors.

Much thanks for the help-that worked!

Will plan on doing that going forward, great tip.