Welcome to the Treehouse Community
Looking to learn something new?
Stuck on Python Basics Challenge
Not sure how to do this one
Q: 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.
Ryan Ruscett23,307 Points
Let me explain this best I can. Thanks for choosing me to answer your question. Ok let's get to it.
Let's look the question and break it down.
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.
Ok, break down.
- Write a function called printer.
- The function takes 1 argument which is counter.
- Print the word "Hi"
- Print the word "Hi" as many times as whatever the counter argument is.
Ok so with that in hand. Let's do it.
- Write a function called printer
We know that to declare a function we need to use def keyword signifying its a function with ().
- The function takes 1 argument called counter. Ok cool.
An argument goes in the () of the function. Meaning that the function needs a value (whatever value is in the()) in order to run do processing.
- Let's print the world Hi.
def printer(counter): print("Hi")
Ok, awesome. We now have a function called printer that takes a single argument counter, and it also prints the word "Hi".
- Print the word as many times as the counter says. We don't know what the counter is but we don't really care do we? As the questions says, we can multiply the string by an integer. A quick few points.
Point 1: print("Hi") we know this is a string, because Hi is surrounded with quotes. "Hi", signifying it's a string. But if am to write the string out x amount of times. x would have to be a number right?. Lets print Hi 5 times or 8 times. I mean I can't print Hi (use another string) "apple" times now can I? No of course not how do I do "Hi" "apple" times. I don't.
So I need to print the word Hi as many times as whatever the value for count is. Which brings me back to the questions which says I can multiply a string by an integer. As in my example above, the string is "Hi" and it only makes sense to multiply that by an integer, since we learned that we can't multiply it by another string, that makes no sense.
def printer(count): print("Hi" * counter)
BAM! We have our answer. We use an Operator called * This operator means multiplications. For example
y = 10 * 10
Y is actually equal to 100. So if I say, hey Peter. I want you to say Hi ten times. You say hi, hi, hi etc. You say it ten times. So the program way to say, say hi ten times is what is Hi times ten AKA (Hi * 10). It's Hi Hi Hi HI Hi Hi HI HI HI Hi.
I hope this helps. I am not sure where your level is in the game of python, so PLEASE feel free to tell me that you have no idea what I am talking about, or that you get it buuuttttt..... I am happy to help in anyway that I can.
Ken AlgerTreehouse Teacher
Welcome to Treehouse!
Let's take it one step at a time.
First, define a function named
printer that takes one argument,
count. We can do that right?
Second, we want to print "Hi" as many times as the
count argument. In Python if we do
'Hi ' * 3 we will get
Hi Hi Hi. So to continue our code then...
def printer(count): print("Hi " * count)
That should do it. Please post back if any of that did not make sense.
Mark Bell7,052 Points
I had this issue. I have noticed once or twice that something comes up in the quiz/code challenge that we haven't covered in the course. In this case, the very next video covered the all important ' * count' action that you were missing in the initial challenge. Kenneth is pretty good though I have to admit and the odd snag is no bother at all. I had never intended to learn Python but a recent IoT project has forced my hand and is proving to be a very smooth addition to my armoury with KL's help!
Patric Daniel Pförtner1,542 Points
You are very close to the answer, here is how I did it:
def printer(count): print("Hi " * count)
Why are you learning Python? Maybe we have the same aims! I am learning it to bring my Start UP www.wolf-gate.com to the next level. Thank´s to Kenneth Love it´s easily possible :)
i tried all of these and none of them were correct
Am i crazy to assume that "count" is just a place holder for an integer?
you can do it with a loop also.
def printer(n): while n: print('hi') n -= 1 printer(4)