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"

Mayank Munjal
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mayank Munjal
Front End Web Development Techdegree Graduate 18,120 Points

Need help with code challenge. Please help!

Write a function named printer. The function should take a single argument, count, and should print "Hi " as many times as the count argument. Remember, you can multiply a string by an integer.

2 Answers

Since this challenge can be a little complicated to newbies, I'll give you two free hints:

Hint one

You should start by defining a function like this:

def printer(count):
    # Find out what goes here...

Hint two

You have to return "Hi" repeated count times. To repeat a string, you can use string multiplication.

Pretend that we're in the Python Shell

>>> "a" * 10
aaaaaaaaaa
>>> "apples" * 3
applesapplesapples
>>> "Hi " * 5
Hi Hi Hi Hi Hi 
>>> count = 5
>>> "Hi " * count
Hi Hi Hi Hi Hi 

I hope this helps. ~Alex

If you continue to fail, please reply. I'd be happy to help more/provide more hints. :smile:

Robert Bryan
Robert Bryan
2,869 Points

I have a related question/comment...

Did not work count(5)

Did work count = 5

Why is this? Was assuming that functions would want the count(5) syntax.

Second question... how did you get commands to display in a terminal looking box in your post?

I can't answer your first question without your code... And for the second question, you can read the Markdown Cheatsheet below a answer/comment box.

I hope this helps.

Robert Bryan
Robert Bryan
2,869 Points

This works

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

count = 5

This does not

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

count(5)

Oh... This is because the code challenge automatically calls the function for you and does not expect you to call it manually. On the other hand, defining a variable doesn't affect how the challenge would call the function. In fact you can completely eliminate the count = 5 line of code.

I hope this helps :grin:

~Alex