1 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
這就是我在評(píng)論中的意思。創(chuàng)建水果副本的原因并不是那么必要。因?yàn)槟鷮淖兞恐袆h除水果,而不是從 .txt 文件中刪除。由你來(lái)決定!
import random
myfruitPicked = ''
fruits = "C:\users\Homer\fruits.txt"
#ASSUMING EACH FRUIT IS IN A DIFFERENT LINE, it is passed into an array (lines).
lines = [line.rstrip('\n') for line in open(fruits)]
fruitsC = lines
while True:
randFruit = random.choice(fruitsC)
myfruitPicked = myfruitPicked + randFruit
#Remove picked fruit from list
fruitsC.remove(randFruit)
if (len(fruitsC)==0): #All the fruits have been picked
print('All Fruits Already Displayed')
break
else:
print(randFruit)
添加回答
舉報(bào)