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"

for "Print hi", can we use for/while loops?

I understand that this challenge asks for the simple multiplication of "hi " string with the integer value of count but i was wondering if there is any other way to execute the same print function using for or while, like the printer_for example on my code (im not even sure if i used for correctly, but still...)

printer.py
def printer(count):
    print("Hi "*int(count))
def printer_for(count):
    for int(count):
        print("Hi ")

1 Answer

Sneha Nagpaul
Sneha Nagpaul
10,124 Points

a. for requires some kind of iterable to loop over. So, using an int won't work.

for some_var in range(int(count)):
    pass

b. print() will usually end with a new line. So, if you want things to show up on the same line you will have to set that in the print function when you call it.

print("Hi ", end = "")

However, if we polish both of these ideas:

for _ in range(int(count)):
    print("Hi " , end = "")
print("")

This might be one way of doing it with a for loop. I use an underscore here because we don't end up using some_var inside the loop and an extra print statement to get the new line effect of the print("Hi "*count) from the original solution.