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 trialShreya Shettigar
351 PointsHow can I define the variable "number" properly?
For this challenge, I am supposed to call the square function and store the value returned in the variable result, but I am not sure how to create the variable.
def square(number):
return number * number
result = square(number)
square(3)
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Shreya,
You're doing the right thing to create a variable: you use the desired variable name, plus the assignment operator (=
) then the thing you want to assign to the variable.
Your mistake is that the expression you are assigning to the variable is invalid: number
is not a declared variable so Python just crashes when it sees your first function call.
Note that the second time you call the function you have the syntax exactly right, but this time you're not assigning the result of evaluating the expression to your result
variable.
If you want to just call square
with a number directly (a so-called 'literal'), you can do exactly what you did on the last line, but prefix that line with the assignment result =
If, as your question title suggests, you want to call square with a value inside the a variable called number
you could, on one line assign a number to the variable number
then subsequently call your function with number
as the given argument (just like you did on your second-from-last line).
Hope that's clear
Cheers
Alex
Shreya Shettigar
351 PointsShreya Shettigar
351 PointsThanks so much Alex! I understood how to solve the problem.
Shreya :)