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 trialMichael Cronk
8,970 PointsI need help with the squared project!
I am honestly completely lost with this one, i don't know where to even start it off. Can anyone help me with this?
def squared(string):
2 Answers
Afloarei Andrei
5,163 Pointsdef squared(string): try: string = int(string) x = string ** 2 except: string = str(string) x = string * len(string) return x
Afloarei Andrei
5,163 Points- Instead of 'if' and 'else' you have to use 'try:' and 'except:'
- You have to convert the function argument "in your case (string)", in integer "int()" and string "str()". You can convert the "string" in, "int()" in "try:", and in, "str()" in "except:"
- Assign the math operation in each situation:
- where you converted the "string" in "int()" >>> x = string ** 2
- where you converted the "string" in "str()" >>> x = string * len(string)
- Return x
Hope you understand what I am trying to say :)
Michael Cronk
8,970 PointsI kind of understand, this is what i got out of it but it says it's wrong.
def squared(string): try: return int(string) x = string ** 2 return x except ValueError: return str(string) x = string * len(string) return x
Michael Cronk
8,970 PointsMichael Cronk
8,970 PointsOhh! I see where i went wrong. Thank you so much!