Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

printer.py
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,275 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
printer(counter)

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

#Or you could just pass in a number
printer(8)

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

Hope this helps. Happy coding.