How to separate each group of five stars by a vertical line?
Question:
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:
*****|**
*****|*****|*****|
Answers:
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))
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()
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)
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:
*****|**
*****|*****|*****|
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))
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()
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)