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: How can I "yell" more efficiently?

In the first lesson on Functions:

Original Solution:

def yell(text):
    text = text.upper()
    number_of_characters = len(text)
    result = text + "!" * (number_of_characters //2)
    print(result)

yell("You are doing great")
yell("Don't forget to ask for help")
yell("Don't Repeat Yourself.  Keep things DRY")

Using methods from previous lessons:

def yell(text):
    text = text.upper()
    number_of_characters = len(text)
    result = text + "!" * (number_of_characters //2)
    print(result)


text = input()
yell(text)

or how about

def yell(text):
    text = text.upper()
    number_of_characters = len(text)
    result = text + "!" * (number_of_characters //2)
    print("Remember:\n\n",result)


print("What's some good advice about writing code?")

text = input()
yell(text)

1 Answer

Steven Parker
Steven Parker
243,656 Points

There's not much to this to be able to optimize it much. You could eliminate the "result" variable by putting the string creation directly inside the print().

Can you give an example?

Steven Parker
Steven Parker
243,656 Points
# instead of this:
    result = text + "!" * (number_of_characters //2)
    print(result)
# you could do this:
    print(text + "!" * (number_of_characters //2))