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 trialManuja Jayawardana
1,450 PointsHow to add "count" to function
How do i add count and print as many time as count applies?
def printer
printer(count)
print "hi"*count
2 Answers
Steve Hunter
57,712 PointsHi there,
You've pretty much done that correctly. You just have some new lines where they're not needed.
Start by def
ining a function called printer
that takes a parameter called count
.
That look like:
def printer(count):
You then want to print "Hi " out as many times as the value held by count
. The question says you can multiple the string.
You want to put "Hi " * count
inside the brackets of the print method. Make sure you indent the line as it is inside the method:
def printer(count):
print("Hi " * count)
That should do it!
Steve.
Kent Åsvang
18,823 PointsOne way to solve this is with a for-loop:
""" First we define a procedure named 'printer' that takes one argument: count """
def printer(count):
""" Then start our foor-loop and take advantage of the built in method 'range()'. range() takes two arguments, for example 2 & 5, and produces a list of numbers from 2 to 5."""
for _ in range(0, count):
print('hi')
You can also solve this with a while-loop:
""" Same as before we declare out procedure """
def printer(count):
flag = 1
while flag <= count:
print('hi')
flag += 1
Hopefully, you understand the logic behind how this works.
Kent Åsvang
18,823 PointsI see that I have misunderstood the task at hand, took your phrasing a little to literal. Anyway, Steve Hunter to the rescue :)
Steve Hunter
57,712 Points
Manuja Jayawardana
1,450 PointsManuja Jayawardana
1,450 PointsThank you! :)