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"

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.