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 Functions and Looping Create a Function

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Returning Value In the body of the f

I am currently stuck on one of task, the question is ... Create a function named square. It should define a single parameter named number. In the body of the function return the square of the value passed in. (A square is the value multiplied by itself. eg: The square of 5 is 25. 25 = 5 x 5)

Could someone provide the answer especially how the concept of squaring number works

4 Answers

You might have a hard time finding someone to just give you the answer but I will try to help guide you to the answer :)

This is the basic layout of a function:

def yourFunctionName(yourParameter):
    do calculations here
    return yourVariable

the square of a number is the number multiplied by itself. ie. 5*5=25 so 25 is the square of 5.

See what you can do with that and let me know if you are still stuck.

GOOD LUCK! :)

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Hi Shawn Thanks for your prompt response. I totally appreciate you guiding me towards the answer This is what i got from the question def square(number): Return math.ceil

From google i can see the the syntax math.sqrt. I understand what square means but not sure about the syntax. Could you provide assistants on this part

Thanks in advance

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sohail Mirza Hi there! You got a really solid answer from @shawndenham. But I feel like you're still misunderstanding the question. You are talking about the sqrt, but that is short for "square root". The square of 4 is 16 because 4 times 4 is 16. Or, 4 + 4 + 4 + 4 = 16. When we talk about the square root, we are talking about which number multiplied by itself gives that number. 4 is the square root of 16. But the challenge isn't asking for the square root, it is asking for the square. If I were to send in 10 to your function, I would expect your function to return 100 because ten times ten is equal to 100.

Hope this helps! :sparkles:

so you don't need to import math to complete this challenge.

I'll give you a little more :)

def yourFunctionName(yourParameter):
    numberSquared = number*number
    return numberSquared

There is the format of the answer. All you need to do is figure out the name of the function and the name of parameter.

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Hi Shawn / Jennifer I managed to work it out def square(int): numberedSquared = int*int return numberedSquared

I am on the second part of the question Great now that you have created your new square method, let's put it to use. Under the function definition, call your new function and pass it the argument 3. Since your square function returns a value, create a new variable named result to store the value from the function call.

Could you provide some additional guidance with regards to this just like the template you provided before

Thanks

Jonathan Trinh
Jonathan Trinh
1,426 Points

did you figure out the answer? as I am stuck on it too

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Hi Jonathan

I gave the answer already above Anyway here it is def Square(int) number = int * int return number

The second part of the question result = square(3)