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 Basics (Retired) Things That Count Things That Count

nazia zaman
nazia zaman
1,410 Points

summary part of changing days into string

How do I do the last part of summary = "I am {} days old!" I changed it to "I am {} days old!".format(days) "I am {} days old!".format(str=days) and a few other ways but to no use. Can anyone help out? thanks!

days_alive.py
age = 27
days = age * 52 * 7
summary = "I am {} days old!"

2 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It is about formatting string.

There are several ways to do this but what you are taught is probably using format() function.

It works as follow.

The placeholder in a string will be substituted by arguments in format() function.

Simplest placeholder is pair of bracket {}. Using placeholder in this way makes it positional placeholder.

The number of argument has to match with number of placeholder (in simplest form)

For example,

my_string = '{} {}'
a = hello
b = world

my_string.format(a, b) # ''hello world"

So to apply this logic to question.

summary = "I am {} days old!".format(days)

You said you did "I am {} days old!".format(days) there is no reason why this shouldn't work.

nazia zaman
nazia zaman
1,410 Points

I will try again and see. But i'm fairly certain I did this a few times and it didn't work.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Here's my passed code while trying to solve your issue.

age = 27
days = age * 52 * 7
summary = "I am {} days old!".format(days)

If the question didn't pass please let me know what the grader said.

nazia zaman
nazia zaman
1,410 Points

Gunhoo Yoon, it worked! thanks so much! :D Although i have no idea why it didn't pass the first time I tried. This is the most basic thing to come to mind and I know i did it! So weird but thanks for saving the day for me! haha :)

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

It could be small typo or connection issue but you get the idea that's all it matters.