Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating promotion code using python

By using python language, what would be a clever / efficient way of generating promotion codes. Like to be used for generating special numbers for discount coupons. like: 1027828-1

Thanks

like image 494
Hellnar Avatar asked Dec 07 '22 04:12

Hellnar


1 Answers

The following isn't particularly pythonic or particularly efficient, but it might suffice:

 import random
 def get_promo_code(num_chars):
     code_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     code = ''
     for i in range(0, num_chars):
         slice_start = random.randint(0, len(code_chars) - 1)
         code += code_chars[slice_start: slice_start + 1]
     return code
like image 135
GreenMatt Avatar answered Dec 27 '22 06:12

GreenMatt