Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Returning Values

Liam White
Liam White
1,290 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))

2 Answers

boi
boi
8,670 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,290 Points

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

urus
urus
610 Points

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.