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

Julia Rix
Julia Rix
3,303 Points

Code Challenge for Things That Count

I have been working on this challenge for an hour just plugging variable and changing syntax to try and make it work. I can get through the first two steps but then the third step shoots me back an error message that reads task 1 no longer works. Can anyone help? I know it must be something simple that I am missing.

age = int("37") days = int("52 * 7") * age summary = ("I am " + days + " days old!")

days_alive.py
age = int("37")
days = int("52 * 7") * age
summary = ("I am " + days + " days old!")

3 Answers

Dan Garrison
Dan Garrison
22,457 Points

Hi Julia,

A few things with your code. First, you don't need to to turn your numbers into strings and then convert them with int(). Instead, just give the value of the variable the number without a quotes. Without the quotes, the number will be an integer by default.

For the summary variable, however, you need to convert the integer value into a string. To do this you will use the str()

There are a couple ways you could do this. I prefer using format, which would like this:

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

You could also do the same thing without the format.

age = 28
days = (age * 52) * 7
summary = "I am " + str(days) + " days old!"

You just have to remember to convert your integer values to a string. Otherwise it won't work.

You don't have to convert to a string when using .format but you do with string concatenation.

Dan Garrison
Dan Garrison
22,457 Points

Ah! Good to know. Thanks!

Jason Anello, sorry I'm a little confused by your comment. Could you explain more?

Dan Garrison
Dan Garrison
22,457 Points

Hi Matt,

Jason is saying that when you use .format, you don't need to use str() to convert the variable to a string. Using .format will do the work for you. If you use string concatenation, like I did in my second example, you have to use `str() to convert your integer values into a string. Does that make more sense?

Thank you so much Dan! That makes perfect sense now.

Thanks for clearing that up Dan!

selected best answer

Julia Rix
Julia Rix
3,303 Points

Thank you Dan! My notes were a bit messy and I didn't quite articulate clearly how to use the str.format() function in them. Guess that's why it wouldn't work. :)

Dave Campbell
Dave Campbell
13,310 Points

One other way to write your summary string would be:

age = 28
days = (age * 52) * 7
summary = "I am %d days old!" % (days)
print summary

You would need to put parentheses around summary because the code challenges are running python 3.x and print is now a function.

print (summary)