0

How to start the iteration loop from 000 to 999 using while loop in python?  

x1=0
x2=999

while x1 <= x2:
    print (x1)
    x1 += 1

don't use for loop, just while loop

Pitto
  • 7,443
  • 3
  • 37
  • 47
Mhmoud Amsha
  • 202
  • 1
  • 2
  • 7

3 Answers3

0

If you're asking how to pad the number with zeros,

print(f'{x1:03}')
Bucket
  • 7,139
  • 9
  • 32
  • 45
0

You could use:

In [1538]: x1=0 
      ...: x2=999
      ...:  
      ...: while x1 <= x2: 
      ...:     print (str(x1).zfill(3)) 
      ...:     x1 += 1 
      ...:             

Prints something like this:

000
001
002
003
004
005
006
007
halfer
  • 19,471
  • 17
  • 87
  • 173
Mayank Porwal
  • 31,737
  • 7
  • 30
  • 50
0

You need something like this:

while x1 <= x2:
     print (f'{x1:03}')
     x1 += 1
Achilleus
  • 1,686
  • 2
  • 18
  • 38