0
即時通訊新手和我正在撕裂我的頭髮在這個循環的問題。我想隨機將一個國家分配給一個人參加抽獎比賽的名單。一旦一個國家被分配了,那麼它應該不再被分配到循環的進一步迭代中。循環應該經歷整個人員列表(比國家列表短)。是的,我知道代碼可能更簡單,但這只是我放棄的一點。請指教!Sweepstake發電機
import random
filez = open ('C:/Users/Andy/Desktop/countrys.txt', 'r')
countries = filez.readlines()
filez2 = open ('C:/Users/Andy/Desktop/names.txt', 'r')
people = filez2.readlines()
def chop (x): #gets rid of wierd added bits
for i in range(0, len(x)):
x[i] = x[i] [:-1]
return x
chop(countries)
chop(people)
def assign (y):
used = []
for person in range(0, len(y)):
num = random.randint(0, len(countries))
if num not in used:
print (people[person] + " gets " + countries[num])
used.append(num)
else:
num = random.randint(0, len(countries))
print (people[person] + " gets " + countries[num])
used.append(num)
assign(people)
輝煌而且非常簡單,謝謝!不知道random.shuffle,列表或zip。需要繼續做我認爲的教程! – user3390486
只要做正確的:http://docs.python.org/3.3/tutorial/ –