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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 35,157 Points

How do I "convert" a string representation of a number into a number for the Code Challenge at the end of the stage?

I am at the end of the Python Basics stage with the number game. I need a hint on how to make the interpreter recognize the string representation of a number for processing in the "squared" function.

I haven't written any code yet because it seems like a pretty straightforward control flow task after that. Either square the number or the number represented by the string OR multiply a string by its own length.

Thanks in advance. Happy Holidays,

2 Answers

Nathan Tallack
Nathan Tallack
22,159 Points

Nancy,

I had the same problem you did when I tried to put the return into the try evaluation. Could not work out why either. I figured it was a quirk of the challenge workspace. That's why I moved the return to the else clause and just tried typecasting the argument to an int in the try evaluation. Seemed to work that way.

While Python is one of the easier languages to learn, it is by no means something that clicks right away for everyone. For me it is a matter of it "clicking" a piece at a time. Class initializers took a while for me to work out. LIst comprehension was crazy hard. And I still struggle every now and then with some of the file operations.

A few things that I have found that make things easier:

  • Going through a course twice to make sure I took in all of the little nuances.
  • Reading questions and answers on the forum to see if I can understand the solutions given.
  • Working out how to answer peoples questions on the forum as a way to motivate me to figure things out for myself beyond the courses.

That last one is why I am here helping you. It is rare that I don't learn something new when working out how to answer someone's question. It is a great way to reinforce what I already know and to work out stuff that I did not quite understand the first time.

Don't give up. That progress you already feel yourself making snowballs and before you know it you'll be addicted. ;)

Nancy Melucci
Nancy Melucci
Courses Plus Student 35,157 Points

Thanks. I am on a relative lull from my day job now and I am working kind of quickly through the tutorials. I do revisit the ones that I need more help with. You are right to encourage that. Practice is hugely important. I am grateful for your help. I just finished the challenge.

Happy New Year and see you here again.

Nancy

Nathan Tallack
Nathan Tallack
22,159 Points

Hi Nancy,

Firstly, congratulations on getting through Python Basics!!! I hope you have had as much fun as I did and keep on going. It only gets better from here! :)

Let's take a look at that challenge.

We are going to write a function that takes a single argument as input.

First we confirm that argument is a number (we verify this by trying to typecast it as an int with the int() function) which we do within a try: statement to see if it throws an exception.

If it does not convert to an int (which we know by the exception that will be thrown if it does not work) we are going to return that string repeated the number of times that its length is (which we learn with the len() function).

If it did convert ok with the try: statement then it will execute the else: block where we will return the number squared (again converting it to an int just incase it is a number as a string).

The code would look something like this.

def squared(arg):  # Here we start our function with a single parameter I assign to "arg"
    try:  # We are testing if arg converts to an int
        int(arg)  # This will cause an exception if it does not work
    except:  # We catch that exception here
        return (arg * len(arg))  # If we got an exception return with the string repeated by its length
    else:  # If we did not catch an exception do this
        return int(arg) ** 2  # Return with the arg (converted to int just in case) squared

Enjoy your next Python course. It is going to be a blast I am sure. And remember, the forum is here to help out at any time. :)

Nancy Melucci
Nancy Melucci
Courses Plus Student 35,157 Points
def squared(arg):
  try:
    return int(arg)**2
  except ValueError:
    return int(arg * len(arg))```

I got something that runs in Workspaces (above) and does everything (except for providing a little extra verbiage when a string is given as an arg - it scolds and then shows the string multiplied by its length.).
Unfortunately all the challenge feedback does is say "Bummer. Try again." 
Given that I am pretty close I wish it would tell me a little more about how to tweak this to pass.

Thanks. You've been a great help. I've struggled with programming and especially with Python for reasons that are unclear and possibly discouraging to me (it's supposed to be easier than other languages.) 

Nancy M.