vendredi 7 octobre 2016

Python : Making a new list from a random list of letters

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']#alphabet
bag_o_letters = []#letters to chose from
letter_count = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1]#random indexes to chose from
for x in range(26):#loops through the random index of letter_count
    for i in range(letter_count[x]):#chooses the index
        bag_o_letter.append(letters[x])#appends the index of the letter to bag_o_letter
        rack = []#list for the person to see
        for a in range(7):#makes the list 7 letters long
           rack.append(bag_o_letter.pop(random.randint(0,len(letters)-1)))#appends the letter to rack(supposedly...)
print(rack)

In this code that you just read it should choose random letters and put 7 of those letters in a rack that the person can see. It shows a error that I've looked over many times, but I just can't see what is wrong.

I put comments on the side to understand the code.

It shows this error:

rack.append(bag_of_letters.pop(random.randint(0,len(letters)-1)))
IndexError: pop index out of range

Can someone please help?




Aucun commentaire:

Enregistrer un commentaire