Search code examples
pythonpython-3.xfunctiondesign-patterns

How to separate each group of five stars by a vertical line?


I'm a new one at the world of Python programming. I've just, unfortunately, stuck on this, I think, simple exercise.

So what I should do is to modify the stars(n) function to print n stars, with each group of five stars separated by a vertical line. I have code like this, but I really don't know what to do with it.

def stars(n):
    for i in range(n):
        print("*", end='')
        if i == 4:
            print("|", end="")

    print()

stars(7)
stars(15)

The output should be like that:

*****|**
*****|*****|*****|

Solution

  • The problem is in the condition. This code should do:

    def stars(n):
    for i in range(n):
        print("*", end='')
        if i % 5 == 4:
            print("|", end="")
    
    print()