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

Python challenge keeps failing though output seems OK.

I am doing a python challenge that I have tried various methods to solve. Here is the latest:

The challenge: Challenge Task 2 of 2

OK, so now use .format() on the string "Treehouse loves {}" to put your name into the placeholder. Assign this to the variable subject again.

My Code:

name='doug'
subject='Treehouse loves {}'
formatted_message = subject.format(name)

The output (from Preview):

Variable    Value
__doc__ None
__loader__  <_frozen_importlib_external.SourceFileLoader object at 0x7fbf62408f60>
create_preview  True
failure 
test_in_same_context    True
traceback   
subject Treehouse loves {}
formatted_message   Treehouse loves doug
__spec__    None
test_number 1
__package__ None
__warningregistry__ {("unclosed file <_io.TextIOWrapper name='/usr/local/lib/ruby/gems/2.3.0/gems/challenge_proctor-0.0.0/lib/challenge_proctor/templates/python.html.erb' mode='r' encoding='ANSI_X3.4-1968'>", , 126): True, 'version': 0, ("unclosed file <_io.TextIOWrapper name='strings.py' mode='r' encoding='ANSI_X3.4-1968'>", , 42): True}
sys 
test_output {}
__cached__  None
__name__    __main__
success 
name    doug

The error message:

Bummer! Be sure to use the {} placeholder and the .format() method.

In the output from Preview, all 3 variables are exactly as I would expect and I have used the format() method with the placeholder '{}', and yet I get the error.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Two aspects the checker is looking for. The correct value of the variable subject and using the format() method on the same line as the string.

name = 'doug'
subject = 'Treehouse loves {}'.format(name)

So, then technically I am not wrong, exactly. I just solved it differently than expected. I guess that's fair.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Yes, you have the technical aspects correct. From the checker perspective if you had coded

subject = subject.format(name)

then you'd have been technically correct. Setting the correct variable to the final value is required so the checker can verify results. In this specific challenge, the checker insists that the format method is on the same line as the string with the {} format field. The extra restriction is to verify understanding of using a method directly on a string. In later challenges, formatting a variable containing the string will be permitted.