Technoob
Technoob

Reputation: 59

Making a loop in python

I have to make a program that when I enter a box_size number it show the box. They user should then be able to put in another number and repeat the process. Only when they type 0 should the program stop.

I've tried adding While True, if, and else statements and breaks but none of them stop the program from running.

#Input
box_size=input("box_size:" )
box_size=int(box_size)
for row in range(box_size):
  for col in range(box_size*2):
    print('*', end='')
  print()
print()

#Output
box_size:6
************
************
************
************
************
************

Upvotes: 0

Views: 70

Answers (2)

YusufUMS
YusufUMS

Reputation: 1493

Try this simpler one. The box_size is initiated 1, then use it in the while loop. As long as the box_size is more than 0, the loop will always be executed.

box_size = 1
while box_size > 0:
    box_size = int(input("box_size:" ))
    for row in range(box_size):
        for col in range(box_size*2):
            print('*', end='')
        print()
    print()

Upvotes: 0

Barmar
Barmar

Reputation: 782653

Put while True: around the code. Then if the user enters 0, break out of the loop.

while True:
    box_size=input("box_size:" )
    box_size=int(box_size)
    if box_size == 0:
        break
    for row in range(box_size):
      for col in range(box_size*2):
        print('*', end='')
      print()
    print()

Upvotes: 2

Related Questions