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 Returning Values

Liam White
Liam White
1,966 Points

Please shed some light on how one would create a function on this quiz question...

Below is one of the quiz questions and it made sense for me as for the answer, but I got stuck on how I would make this a function. How would you create this code as a function so one wouldn't repeat the code?

first_number = 5 first_result = first_number * first_number print("The number {} squared is {}".format(first_number, first_result))

second_number = 8 second_result = second_number * second_number print("The number {} squared is {}".format(second_number, second_result))

3 Answers

boi
boi
14,241 Points

You're currently on "Returning values" video right?. Just after this video is a challenge to create a function that would take any number and return the square of it. So basically the question you asked here, you will answer yourself once you solve that challenge.

If I help you by providing a solution to the question you asked, it would mean I solved the challenge for you. I recommend you try to solve the challenge, If you need help, I'll always be there to 🔦 shed some light 🔦

Liam White
Liam White
1,966 Points

Thank you! 🤜🤛 I’ll let you know if I need any help after I complete that challenge.

Hi Liam. I was stuck for a moment on that challenge as well. It may be helpful to review the video in Variables and then Functions. Best hint I can give you is to keep it simple... and clean.