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

Regex "The Basics" First 5 objectives Something strange I noticed Don't know if its intentional or not??

Here's the challenge: http://teamtreehouse.com/library/regular-expressions-in-python/introduction-to-regular-expressions/the-basics

Challenge Task 1 of 5 Use open() to load the file "basics.txt" into the variable file_object.

I use this code:

file_object = open("basics.txt", encoding="utf-8")
data = file_object.read()
file_object.close()

The above code gets me past the first 3 challenges

Notice I'm not using this import statement:

import re

..at the beginning of the code.

Then I get to:

Challenge Task 4 of 5

..where it explicitly tells me to do the import:

Import re. Create an re.match() for the word "Four" in the data variable. Assign this to a new variable named first.

So what happens if I add the fourth line of code without the import re.

file_object = open("basics.txt", encoding="utf-8")
data = file_object.read()
file_object.close()
first = re.match('Four', data)

I know this fourth line is correct will pass,

if import re was included

However...(the plot thickens..)

The error I think I should get is: Bummer! Did you forget to use import re

Instead the error I get is:

Oops! It looks like Task 1 is no longer passing.

Weirder still...

If I use these code lines (which included the import re line of code)

import re

file_object = open("basics.txt", encoding="utf-8")
data = file_object.read()
file_object.close()

the first 3 challenges pass just fine (even though it never told me to use import re until the 4th of 5th challenge)

Hmmmm...is this the usual (expected) behavior? (just wondering...)

From what I've seen not to many people have posted forum posts using these search parameters: https://teamtreehouse.com/forum/syllabus:1132 https://teamtreehouse.com/forum/stage:4612

..so this small 'glitch' might not have been picked up by anyone else..

6 Answers

import re file_object = open("basics.txt", encoding="utf-8") data = file_object.read() file_object.close() first = re.match('Four', data) libery = re.search ('Liberty', data)

I don't think you can take the treehouse as the replicating correctly python/php etc.. However did you click on the view button (forgot whats it called editor/view output or something, Just to the right of check work).. that should give you more correct and thorough errors.

I sometimes have a connection problem with treehouse when i am on stages 3 or 4. so what I do is i copy the entire code and refresh & paste.. and just press check work until I get to the stage where i left off and treehouse DOESNT moan why there is extra code.. I think their criteria is that it does what they require you at that stage AND there is no compiler error.. for example if you included import somethingTHatdoesntExist... it would give you an error..

import re

file_object = open("basics.txt", encoding="utf-8") data = file_object.read()

file_object.close()

Bummer! Did you close the file? whats going on?

Still no update on this answer in other new posts? Kenneth Love?

this one works for me

file_object = open("basics.txt")

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Why would a currently unneeded import cause the challenge to fail? It's just unnecessary code, it's not a bug. You wouldn't expect the challenge to fail because you wrote a completely unnecessary and unreferenced function, would you?

And, shez azr there's not really any "replicating" going on. We take your code and run it through Python to make sure it actually does what it's supposed to do.

fair enough.. If that is the case then your vague errors at the top led me to think so but then I also mentioned you get correct errors if you click on preview button..

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Let me know what errors you find vague and I'll try to improve them.