Home > database >  Each group of five stars separated by a vertical line
Each group of five stars separated by a vertical line

Time:10-31

I'm a new one at the world of Python programming and I 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:

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

CodePudding user response:

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()

CodePudding user response:

Trickier than expected...

def stars(n):
    solution = ''
    for i in range(n):
        if i % 5 == 0 and i != 0:
            solution  = '|'
        solution  = '*'

    if n % 5 == 0:
        solution  = '|'

    return solution

print(stars(7))
print(stars(15))

CodePudding user response:

The code would look like this

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

stars(7)
  • Related