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 trialJoey Feldhaus
2,220 PointsIve 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.
def printer(count):
return(count)*str("Hi ")
avg=printer(15)
print(avg)
2 Answers
Cena Mayo
55,236 PointsHi 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
Treehouse Moderator 68,441 PointsSorry. Hit best answer by mistake.
Chris Freeman
Treehouse Moderator 68,441 PointsSurprisingly, both orderings work:
>>> "Hi" * count
'HiHiHi'
>>> count * "Hi"
'HiHiHi'
>>> "Hi" * count == count * "Hi"
True
Sorel Clemens
5,797 Pointsthanks for checking
Sorel Clemens
5,797 PointsSorel Clemens
5,797 PointsYou're code works(it prints 15 times Hi) but you misunderstood the question. You should use print inside the fuction ;)
[MOD: added ```python formatting -cf]