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 (2015) Logic in Python Print "hi"

I don't understand

I don't know how to * it or use the right variable. If you understand please help.

printer.py
def printer(count): 
    int(printer(count)*print('Hi '))

1 Answer

Ben Reynolds
Ben Reynolds
35,170 Points

You can simply multiply a string by a number in a print statement and it will repeat, like this:

def repeat_string(num):
    print("Some string" * num)

If you wanted to cast it to an int (not needed for this exercise, but still a good idea in a real app) you'd probably want a try/catch like this:

def repeat_string(num):
    try:
        int(num)
        print("Some string" * num)
    except ValueError:
        # this will run if an exception is caught