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!

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 Functions

how to keep the last quiz question dry?

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))

I try to find a function and how to re-code this copy pasta to make it DRY but cant think of any.

Please help!

2 Answers

Steven Parker
Steven Parker
225,770 Points

One of the best tools for DRY coding is functions. Combine repeated code into a function and call it as needed:

def doSquare(num):
    square = num * num
    print("The number {} squared is {}".format(num, square))


Thank you!

Steven Parker
Steven Parker
225,770 Points

Gia Hoang Nguyen — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!