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 trialCamron Phillips
447 PointsHow would you fix the "code smell" of this 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))
Couldn't figure it out myself :( :(
2 Answers
Eric M
11,546 PointsHi Cameron,
As Kris has mentioned the issue with this code is that you're repeating the same code. In most cases you want to keep code DRY (Don't Repeat Yourself).
There are actually two tasks to functionally abstract in the example you've posted (1 - square a number, 2 - print a message). Depending on the behavior that you want these could be together in a single function or separated into multiple functions.
So, here's the original:
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))
Let's take a look at doing it all together in one function. What do we want this function to do? It needs to take a number and print the number and its square.
def print_square(number):
square = number * number
print("The number {} squared is {}".format(number, square))
print_square(5)
print_square(8)
That's pretty straight-forward, but what if we wanted to use the square for something else? We don't have it anymore in our main scope as square
is a local variable to the print_square()
function.
We could break this into two functions and still keep things dry
def square(number):
return number * number
def print_square_message(number, sq):
print("The number {} squared is {}".format(number, sq))
first_number = 5
second_number = 8
first_square = square(first_number)
second_square = square(second_number)
print_square_message(first_number, first_square)
print_square_message(second_number, second_square)
KRIS NIKOLAISEN
54,972 PointsIt is the same code (squaring a number) repeated only with different variable names. In this case you would create a function and have it accept as a parameter a number to square.
Camron Phillips
447 PointsI understand i need to create a function. I was wondering what this code would look like, couldn’t figure it out myself
Camron Phillips
447 PointsCamron Phillips
447 PointsThank you! Exactly what I was looking for.