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 trial

Python Python Basics (2015) Logic in Python Print "hi"

Functions

I am not sure what a function is, when it said write a "function" named printer, in my head i was like did you mean write a "variable" named printer?

i also tried printer = int(input("hi ")) printer * 3 is it possible to use a similar format like int input to get similar results? cause it kinda makes sense to me but it might be missing something... if not what practical purpose does it have?

overall the thing i wrote i don't quite understand as well. how do i write it where it makes more sense?

printer.py
def printer(count):
    return(count) * 3
Hi = printer("Hi ")
print(printer)

I've been stuck on this, could someone give me a clear example. (prays)

1 Answer

Robert Goddard
Robert Goddard
15,019 Points

Alright, a function is simply a block of code that you can call with parameters which will do something and may or may not return something... So let's look at your code that you wrote:

I'll mark up what is what:

def printer(count):     // Function Definition (accepts count as a parameter)
    return(count) * 3    // Function "do something" (in this case it multiplies the count passed in by 3 and immediately returns the result)
Hi = printer("Hi ")    // This is essentially setting a new variable "Hi" to be the result of "Hi " * 3. Which may result in Hi Hi Hi
print(printer)   // not sure what this does

The way I would write a complete program to write "Hi Hi Hi" to the screen would be something like this:

// Define the method that accepts a repeatCount (How many times to repeat the word) and a word (The word that is repeated)
def repeatWord(repeatCount, word):
    return repeatCount * word

// Using the function
myRepeatedWord = repeatWord(4, "Hello ")
print(myRepeatedWord)  // Should look like this on the console output Hello Hello Hello Hello

dude thankyou so much this is really helpful you blew my mind haha