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!

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"

def Function. count

Hi, Check out my answer to this challenge. I would think the shell would just print Hi once; but it does not. I am assuming that the argument 'count' is for intergers only and "Hi " is a string is this is why it won't print? But where would I add the int() part? the def or the print?

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

forgot to add challenge question. 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.

1 Answer

Agustin Fitipaldi
Agustin Fitipaldi
1,644 Points

You're supposed to print it out multiple times, so

counter = 10
#you have to set the value for the counter
def printer(counter):
        print("hi" * counter)
        #this multiplies your string by the counter value

What you said makes perfect sense. I am hoping that once I get more used to Python (still my 1st programming course ever), I will be able to understand that the challenge wants more than what they ask.

Ryan S
Ryan S
27,276 Points

Hi test account,

Since you mentioned you are just beginning your programming journey, I just wanted to add one small correction to clear up any confusion going forward.

It is important to not to go beyond what the challenge asks and to not define variables that you are not asked to because it could lead to overriding things you shouldn't override. So to address your point about the challenge wanting more than it asks, this is actually not true. It is quite the opposite. You should only do what it asks, nothing more.

While the function that Agustin wrote is completely correct, it is unnecessary to define the counter variable beforehand for 2 main reasons:

1) The challenge does not ask you to create a counter variable, and furthermore, it does not tell you how many times it wants the string printed.

The code checker will call your function and pass in whatever values it wants to. We don't know what they are. All we know is that a single integer will be passed in each time it is called. That single integer will then become the value of your argument, "counter", inside the function.

2) Even if you define a "counter" variable, it is not automatically passed into the function definition.

Just because the variable and the function argument share the same name does not mean that counter = 10 will be used in your function.

To use the counter variable, you'd pass it in when you call your function:

counter = 10

# Also, the variables don't need to have the same name as the argument
new_counter = 15

#Or you could just pass in a number

But again, you don't need to supply these arguments in the challenge. This is just for demonstration.

Hope this helps. Happy coding.