codebarre(“5011386029399”) affiche le code à barres EAN13 correspondant.
Attention, un code à barres erroné est refusé.
from kandinsky import set_pixel,draw_string def codebarre(ch="5011386029399"): s=0 for i,e in enumerate(ch): s+=int(e)*(1+2*(i%2)) s%=10 if s!=0: raise ValueError("Code errone") codes={"L":["0001101","0011001","0010011","0111101","0100011", "0110001","0101111","0111011","0110111","0001011"]} codes["R"]=["".join({"0":"1","1":"0"}[c] for c in l) for l in codes["L"]] codes["G"]=["".join(l[-i-1] for i in range(7)) for l in codes["R"]] choix=["LLLLLL","LLGLGG","LLGGLG","LLGGGL","LGLLGG", "LGGLLG","LGGGLL","LGLGLG","LGLGGL","LGGLGL"] barres="101" gauche=choix[int(ch[0])] for i in range(1,7): barres+=codes[gauche[i-1]][int(ch[i])] barres+="01010" for i in range(7,13): barres+=codes["R"][int(ch[i])] barres+="101" i=53 for j,e in enumerate(ch): draw_string(e,i,162) if j in {0,6}: i+=21 else: i+=14 for i,e in enumerate(barres): if e=="1": if i in {0,2,46,48,92,94}: l=111 else: l=100 for j in range(l): set_pixel(65+2*i,j+60,(0,0,0)) set_pixel(66+2*i,j+60,(0,0,0))