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"

Joey Feldhaus
Joey Feldhaus
2,220 Points

Ive been trying for 15 mins...Idk what to do

Any help would be appreciated. I want to define a function that lets me print "Hi " as many times as the input.

printer.py
def printer(count):
  return(count)*str("Hi ")
avg=printer(15)
print(avg)  
Sorel Clemens
Sorel Clemens
5,797 Points

You're code works(it prints 15 times Hi) but you misunderstood the question. You should use print inside the fuction ;)

def printer(count):
  print("Hi"*count)

[MOD: added ```python formatting -cf]

2 Answers

Hi Joey,

You're looking for this:

def printer(count):
    print("Hi" * count)

The string has to come first, as you can multiply it by the 'count' number. If you try to put count first, you're basically saying '5 times "hi", rather than "Hi times 5". The first doesn't make any sense to Python. Also note that your additional code isn't necessary.

Hope that clarifies things!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Surprisingly, both orderings work:

>>> "Hi" * count
'HiHiHi'
>>> count * "Hi"
'HiHiHi'
>>> "Hi" * count == count * "Hi"
True