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 trialoliverchou
20,886 PointsAnyone help me out?
When the system executed squared(tim) it would be a NameError .However,if it executes squared("tim") ,my code works! So anyone got ideas about how to turn a name of the variable(ex:tim) into a string, but not turning the value of "tim",the name of the variablewhich I've never named, into a string(thus caused a NameError)? Or is there any reason why I could't pass the challenge?
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(x):
try:
return int(x) ** 2
except ValueError:
return str(x) * 2
2 Answers
Rick Buffington
8,146 PointsWhat you are asking for is a roundabout way of using a variable. Here is your problem:
squared(tim)
The above is looking for a variable that is named tim.
squared("tim")
The above sees a string that is passed to it. No need to look for anything, it just squares the word "tim".
To accomplish what you are asking, you would have to do something like this:
tim = "tim"
squared(tim)
That would then spit out "timtimtim" because the variable tim is equal to the string "tim".
Hope that helps. Let me know if you still have questions.
Adam Akers
7,357 PointsI think you might be getting that because tim
is not yet been declared. When you are calling the function squared(tim)
is it looking for a variable, by the name of tim, however in your code here, there is no tim so it will throw an error. Passing in the string works because that would be a valid argument.
Try adding the variable tim = "tim"
at the top of your code and then run it again and hopefully it works.
oliverchou
20,886 PointsI know that works. However,my goal is to pass the challenge. It doesn't meet the challenges demand,and I got no idea what's wrong with it! I'm currently stuck in this stage! thanks,anyway!
oliverchou
20,886 Pointsoliverchou
20,886 PointsI knew the difference of the above, but the challenge's demand is to get "timtim" when execute: squared(tim) How could I make this possible?
Rick Buffington
8,146 PointsRick Buffington
8,146 PointsOkay, I see your dilemma. You are close, however they want you to spit out the string times the length of the string. So it would be something like:
return str(x) * len(str(x))
This will multiply the string * the value of the length of the string. That should get you past the challenge. The directions are slightly confusing - I had to read them like 5 times. :) Hope this helps.
oliverchou
20,886 Pointsoliverchou
20,886 PointsI've fixed it! The direction is really confusing! Thanks! :)