Printing pattern using while loop

Can you see how the above example works? As an exercise, try printing the following pattern using a while loop (Hint: use string concatenation):

          *
         **
        ***
       ****
      *****
     ******
      *****
       ****
        ***
         **
          *

i could solve this using 2 nested while loops , but how to solve using string and concatenate ?

2 Likes

I tried doing it by changing the jovian module example a bit,
this solution has 2 while loops.

def rev_star(n):
  line = '*'

  while len(line) <= n-1:
      space = n-len(line)   # Counter for number of spaces
      print(' '*space, line)   # Statement to print required number of spaces and line(*)
      line += "*"
      
  while len(line) > 0:      # Similar loop but for 2nd half of the pattern
      space = n-len(line)
      print(' '*space, line)
      line = line[:-1]

I tried to do it with 1 while loop,

def rev_star2(n):
  line = '*'
  i = len(line)

  while i <= 2*n-1:
    space = n-i

    if(i>n):  # Condition for decreasing 2nd half
      print(' '*-space, line*(2*n-i))   # -space to print spaces for the decreasing 2nd half
    else:
      print(' '*space, line*i)          # space to print spaces for increasing 1st half
    i += 1

But I don’t think I’m using string concatenation in the 2nd example(rev_star2).

Output for both:
rev_star(6) and rev_star2(6)

      *
     **
    ***
   ****
  *****
 ******
  *****
   ****
    ***
     **
      *

Edit:
I tried the 2nd example without using i=len(line) and that does have string concatenation,

def rev_star3(n):
  line = '*'

  while len(line) <= 2*n-1:
    space = n-len(line)

    if(len(line)>n):  # Condition for decreasing 2nd half
      print(' '*-space, line[:2*n-len(line)])   # -space to print spaces for the decreasing 2nd half

    else:
      print(' '*space, line)          # space to print spaces for increasing 1st half

    line += '*'       # String concatenation
2 Likes

line = β€œ*”
max_length = 8

while len(line) <= max_length:
print(" " * (max_length - len(line)) + β€œ" * len(line))
line += "
”

while len(line) > 0:
print(" " * (max_length -(len(line) - 2)) + β€œ*” * (len(line) - 2))
line = line[:-1]

1 Like

I wrote this as a solution for the first, is what I’m doing with rewind cheating? ahah

ast = "*"
space = "     "
rewind = False

while len(ast) <= 5:
    print(space + ast)
    ast += "*"
    space = space[:-1]

if len(ast) == 6:
    rewind = True

while rewind == True and len(ast) >= 1:
    print(space + ast)
    ast = ast[:-1]
    space += " "

And this as a solution for the second:

ast = "*"
space = ""
max_lenght = 11
rewind = False

while len(ast) < max_lenght:
    space = " " * ((max_lenght - len(ast)) // 2)
    print(space + ast)
    ast += "**"

rewind = True if len(ast) == max_lenght else False

while rewind == True and len(ast) > 0:
    space = " " * ((max_lenght - len(ast)) // 2)
    print(space + ast)
    ast = ast[:-2]

No, it’s not cheating to use rewind, as a marker.

Generally, flag=0 or flag=1 is used to keep a mark on which section of the program to run.

1 Like

Hi, I did this for the first problem, and it worked. However, I am still new to this, and I am not sure about the exact meaning of [:-1], is it like subtracting 1 each time? Thanks. (Space should be six spaces).

line = β€˜*’
space = " "
max_length = 6

while len(line) <= max_length:
print(space, line)
line += β€œ*”
space = space[:-1]

while len(line) > 0:
print(space, line)
line = line[:-1]
space += " "

Instead, for the second problem I did this. (Space should be six spaces).

line = β€˜*’
space = " "
max_length = 10

while len(line) <= max_length:
print(space, line)
line = line + β€œ**”
space = space[:-1]

while len(line) > 0:
print(space, line)
line = line[:-2]
space += " "

line[:-1] returns all the values stored in the line variable without the last value, what comes before the β€œ:” determines where is starts to pick up values, and if nothing is before the β€œ:”, it means it will pick from the first value to the right value not comprised, where the right value is the one stated after the β€œ:” sign. As -1 is used to indicate the last value of a variable, this means that β€œline[:-1]” will return every value stored in the variable line untill the last value (not comprised).