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 (2015) Number Game App Squared

I 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?

squared.py
def squared(string):

2 Answers

Afloarei Andrei
Afloarei Andrei
5,163 Points

def squared(string): try: string = int(string) x = string ** 2 except: string = str(string) x = string * len(string) return x

Ohh! I see where i went wrong. Thank you so much!

Afloarei Andrei
Afloarei Andrei
5,163 Points
  1. Instead of 'if' and 'else' you have to use 'try:' and 'except:'
  2. 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:"
  3. 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)
  4. Return x

Hope you understand what I am trying to say :)

I 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