mercredi 2 décembre 2015

How to eliminate an item from a list after using random.choice()?

So i have a python script with a list that i am trying to get random items from the list and put them in a variable, But i have noticed that when i run the program a few times (about 20 or so times) eventually it will print out 2 of the same items like "apples apples".

import random

list = ['apples','grapes','bannas','peaches','pears','oranges','mangos']
a = random.choice(list)
b = random.choice(list)
while a in (list[0],list[1],list[2],list[3],list[4],list[5],list[6]):
    a = random.choice(list)

while b in (list[0],list[1],list[2],list[3],list[4],list[5],list[6]):
    b = random.choice(list)

print(a + ' ' + b)

The while loop should cause the variable to contain a unique value each time but it does not.




Aucun commentaire:

Enregistrer un commentaire