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

Kevin Martinez
Kevin Martinez
7,711 Points

Is there anything wrong or bad practice about the way i answered this?

Hi! So I wrote my code a bit different than the answer:

def two_plus_two():
    val = 2 + 2
    return val

sum = two_plus_two()
print(sum * 2)
```python

It was still able to print out 8. Is there anything wrong or bad practice about the way I thought about? Just curious, thanks!

3 Answers

Steven Parker
Steven Parker
229,783 Points

There's rarely only one "right way" to program a solution. Since you didn't provide a link to the original material, I can't evaluate your code in reference to the stated requirements — but clearly the first function does return the result of adding two to itself as the name implies. But it could be more concise by skipping the creation of the variable:

def two_plus_two():
    return 2 + 2
Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

Hi Kevin Martinez! As I read your script, I thought you could use parameters to make it more "Pythonic" - that is, concise and easy to understand.

def two_plus_two(num1, num2):
    val = (num1 + num2) * 2
    print(val)

I hope this helps!

Kevin Martinez
Kevin Martinez
7,711 Points

sorry for not referencing the original material, I used the community link under beginning python so i figured it would auto-associate to the lesson i was on. Thanks for the answer!