vendredi 22 février 2019

Python Random display and store dont repeat stored value

I'm struggling to get my code to work
I have a large list of fruits stored on my PC. Any time i run the program, i want the program to randomly display one fruit from the list. And then save that fruit in myfruitPicked.

My objective is, The program should always check myfruitPicked if the randomly selected fruit is already in myfruitPicked. then it should discard that fruit and randomly pick another one from the original list. when the fruits in myfruitPicked list is equal to the number of fruits in my original list. the program should break and print all fruits have been displayed.

The idea is, I dont want to see one fruit displayed twice anytime i run the program. Also because the fruit list is so large. I want to code to work well so it doesn't cause memory problem or slow that the program. Thanks for your help in advance

import random
myfruitPicked = ''
fruits = "C:\users\Homer\fruits.txt"

while True:
     randFruit = random.choice(fruits)
     myfruitPicked = myfruitPicked + randFruit

     if randFruit in myfruitPicked:
         print('All Fruits Already Displayed')
         break
     else:
         print(randFruit)




Aucun commentaire:

Enregistrer un commentaire