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"

Greg Isaacson
PLUS
Greg Isaacson
Courses Plus Student 702 Points

Please help!

Hi All...

Still getting used to writing my own functions

Please help me with this code...what am I doing wrong???

As I understand functions, you need to start with:

def "insert function name"()

My count is = 10

then print ("hi" * count or 10).

Then I need to call it again printer()...

thanks!:)

printer.py
def printer():  
    count = 10 
    print("hi"*count)
    printer() 

The way this function would work, you would print 'hihihihihihihihihihi' every time you called printer(). What if I wanted to print 'hi' twenty times? I couldn't use this same function.

1 Answer

Umesh Ravji
Umesh Ravji
42,362 Points

Hi Greg, your almost there, just two issues.

1. The printer function should take one argument count so make sure to include that in the definition too. Since this is passed into the function, you shouldn't declare a local variable count, so remove that.

2. You don't need to call the printer function again.

def printer(count):  
    print("hi" * count)
Greg Isaacson
Greg Isaacson
Courses Plus Student 702 Points

Ok Starting to understand this better...

So def printer(This is kind of like giving a variable a name): print("hi" * count) --->> This "connects" the number of hi's in the print command to the value in count IN the () of def printer.

Hope im understanding this correctly:) Thanks!

Greg,

I think you are getting it!

Define a function named printer. When you call the printer function you know you will pass in a number so your function can print 'hi' as many times as that number is. For that reason you put in an argument that you name count. Now you have a function you can use the same function multiple times for different reasons.

ten = 10
one = 1

def printer(count):
    print('hi ' * count)  # notice the space after 'hi_'

printer(ten)  # this will print 'hi hi hi hi hi hi hi hi hi hi '
printer(20)  # this will print 'hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi '
printer(one)  # this will print 'hi '

This is the beauty of functions. Making your life easier and not having to repeat the same code multiple times. Can you take this small function to another level? What if I wanted to input a string of my choosing into the printer function, how would you do that?

Hint, you will need to replace 'hi ' with something else.