我不擅長編碼,我正在努力改進和學習。我試圖編寫一個隨機選取6個非重複數字的代碼,但是我沒有做到這一點。我該怎麼辦?製作6個不同的隨機數字
import random
a = random.randint(1, 100)
b = random.randint(1, 100)
c = random.randint(1, 100)
x = random.randint(1, 100)
y = random.randint(1, 100)
z = random.randint(1, 100)
outa = b, c, x, y, z
outb = a, c, x, y, z
outc = a, b, x, y, z
outx = a, b, c, y, z
outy = a, b, c, x, z
outz = a, b, c, x, y
all = a, b, c, x, y, z
while a in outa or b in outb or c in outc or x in outx or y in outy or z in outz:
if a in outa:
a = random.randint(1,100)
elif b in outb:
b = random.randint(1,100)
elif c in outc:
c = random.randint(1,100)
elif x in outx:
x = random.randint(1,100)
elif y in outy:
y = random.randint(1,100)
elif z in outz:
z = random.randint(1,100)
print(all)
你瞭解列表什麼? – Matt
*「隨機選取6個非重複數字」*:'random.sample(range(1,100),6)' –
這個問題中使用'sample'(除我的)之外的所有建議都缺少100個範圍。 '範圍'不包括結尾值,但'randint'確實。 – rodrigo