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"

printer.py

I have a few questions about this quiz.

I passed the challenge by typing these codes

def printer(arg):
    count = int(arg) * "Hi "
    print(count)

but I saw the code looks like this in the video

def average(num1, num2):
    return (num1 + num2) / 2

avg = average(2, 8)
print(avg)

if I try to type these codes into challenge shell

def printer(arg):
    count = int(printer(arg)) * "Hi "
    print(count)

or

def printer(arg):
    arg = 2
    count = printer(arg) * "Hi "
    print(count)

it won't be passed

I'm a kind of confusing to understanding the different. Need help, guys. Thx.Chin.

printer.py
def printer(arg):
    arg = 2
    count = printer(arg) * "Hi "
    print(count)

1 Answer

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

This is probabbly recursive:

def printer(arg):
    count = int(printer(arg)) * "Hi "
    print(count)

printer(arg) will call the function it is in again and again and never stop, as it keeps calling it self.

Below, it is correct as printer is called, outside the function, with the argument set to 8. Using return to send back the result to the script, rather than print it out in the function.

def printer(arg):
    count = int(arg) * "Hi "
    return count

anyname = printer(8)
print(anyname)

Thank you so much. Dear Christopher.