0

I am trying to make a program print all possible 4 digits pin codes.

My program:

Pin = 0000
print (Pin)
while Pin < 10000:
    print (Pin)
    Pin = Pin + 1

But it start at 0 and then continue 1, 2, 3, 4 and so on.

How do I make it start at 0000 and continue 0001, 0002 - 9998, 9999?

DavidG
  • 24,279
  • 14
  • 89
  • 82
WolffSPF
  • 3
  • 3
  • 1
    Take a look at similar question https://stackoverflow.com/questions/134934/display-number-with-leading-zeros – Vadim Kotov Feb 14 '18 at 09:46
  • If you want `02` and `0002` to mean something different, you need to have a different format, e.g. a string `'0002'`, or a tuple `(0, 0, 0, 2)` – Peter Wood Feb 14 '18 at 09:49
  • Incidentally, integer literals starting with `0` are interpreted as octal and are only allowed to use digits `0` to `7`. See [What do numbers starting with 0 mean in python?](https://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python) – Peter Wood Feb 14 '18 at 09:51

2 Answers2

4

If you want to print from 0000 to 9999 try

#If not python3 uncomment line below
#from __future__ import print_function
for i in range(10000):
    print("{:04d}".format(i))
KolaB
  • 501
  • 3
  • 11
3

Use the function zfill():

print str(Pin).zfill(4) 
DatGeoudon
  • 342
  • 3
  • 15