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!
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
Anyone 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
Rick Buffington8,105 Points
What you are asking for is a roundabout way of using a variable. Here is your problem:
The above is looking for a variable that is named 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.
Rick Buffington8,105 Points
Okay, 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.
I've fixed it! The direction is really confusing! Thanks! :)
Adam Akers7,357 Points
I 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.
I 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!
I 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?