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

or porat
or porat
684 Points

Can't pass this question

Hey! first question: can i convert "apple" to an Int? and if so did i make it right? second question, what's wrong with my cod?

Thanx!

squared.py
def squared(apple):
    int(apple) = 5
    return apple * 4



# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

2 Answers

Alex Arzamendi
Alex Arzamendi
10,042 Points

Hi there! the issue in here comes from the case in which the user passes something other than a number!

So lets say for example that the user passes in the string 9, this can be converted to a numeric value by using a converter which is the case of the int(function) but in the event of the user passing a string the program would crash. In order to avoid this we use the try-except block and for this we need to know the type of exception that might be raised, if you try your function as it stands in the Python interpreter then you would see that the error referenced is ValueError, so we can simply wrap this up in a nice try block like this

def squared(number)
    try:
        number = int(number)
        return number * number
    except ValueError:
        return len(number * number

Because of this, the program will not crash and if you pass a string of characters as a parameter it will know what to do with the raised exception.

Now, notice your assignments as well int(apple) = 5 makes no sense, if you want to pass 5 to the function, that is where the argument call goes as demonstrated in the examples, thus using squared(5) will put the value of number to 5, also, because you are not saving the reference to int(number) anywhere then the value would be pretty much useless. Reread the concepts regarding value assignment if you are still sketchy on this part. Basically, if you want to transform valriable number = "9" to a number value AND use it, you would do

# we have a number variable that has a string value that represents a number

number = "9"

# we want to change that to a number and use it

number =  int(number)
Steven Parker
Steven Parker
231,269 Points

I think you meant to say, "the program will not crash ...".

Alex Arzamendi
Alex Arzamendi
10,042 Points

yup, corrected that already, thanks for pointing that out!

or porat
or porat
684 Points

Thanx alex! i think i got it thanks to you! but still im trying to do it exacly howd you show me but still its doesnt worked.. its said: can't multiply sequence by non-int of type 'str'

Steven Parker
Steven Parker
231,269 Points

:point_right: Alex's example has a couple of syntax errors.

Here it is with proper syntax:

def squared(number):
    try:
        number = int(number)
        return number * number
    except ValueError:
        return len(number) * number
or porat
or porat
684 Points

thanx alex!

Alex Arzamendi
Alex Arzamendi
10,042 Points

just one actually :v the parenthesis :v must have taken it off by accident