Home > Software design >  Random license plate generator
Random license plate generator

Time:05-19

There is random license plate generator. It generates license plates with 3 letters and 3 numbers but it excludes letters Q, W and X in all three letter positions, and 000 in number position. I tried to code this on Python but it showed an error message:

IndexError: list index out of range
The code is this:
import random as o
r=o.randint
t,j,k=[],0,""
b=["Q", "W", "X", "000"]
for i in range(100):
 l,j=b[0],b[4]
 while any(w in l for w in b):
  l,j="",""
  for i in range(3):
   l =chr(r(65,90))
   j =str(r(0,9))
 t.append(l '-' j)
print ("\n".join(set(t)))

CodePudding user response:

l,j=b[0],b[4]

Here you're getting index 4 but you're only using length of 4, so the last item has index 3, change it to:

l,j=b[0],b[3]

Try it online!

Output:

MLD-767
SMP-526
PIR-280
JBY-473
MRZ-876
VEN-206
FNI-643
ULH-631
ZCK-707
...

CodePudding user response:

There are many ways to do this. Here's another one:

from random import choices, randint

LETTERS = 'ABCDEFGHIJKLMNOPRSTUVYZ'

def gen_number_plate():
    letters = ''.join(choices(LETTERS, k=3))
    numbers = randint(1, 999)
    return f'{letters}-{numbers:03d}'

print(gen_number_plate())
  • Related