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 trialor porat
684 PointsCan'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!
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
10,042 PointsHi 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
231,269 PointsAlex'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
684 Pointsthanx alex!
Alex Arzamendi
10,042 Pointsjust one actually :v the parenthesis :v must have taken it off by accident
Steven Parker
231,269 PointsSteven Parker
231,269 PointsI think you meant to say, "the program will not crash ...".
Alex Arzamendi
10,042 PointsAlex Arzamendi
10,042 Pointsyup, corrected that already, thanks for pointing that out!
or porat
684 Pointsor porat
684 PointsThanx 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'