我正在创建一个乐透计划,在那里它可以抽取8个数字,并检查它们是否和100个乐透玩家的数字匹配。我已经创建了随机生成8个数字并将其存储在列表中的部分,但是我不知道如何在python中创建一个2D列表来存储用户随机生成的数字及其ID
例如,2D列表应该如下所示
Player ID | PlayerNumber
1 | 2 3 4 5 6 7 4 23
2 | 14 12 14 30 0 13 25
3 | 21 30 2 22 23 14 19 12
4 | 21 12 22 30 24 25 28 15
8 numbers winning numbers are from a pool of 0 - 30 numbers
the Player ID pool is 1 - 100 meaning 100 players
the Lotto number pool is 0 - 30
我创建的部分绘制了8个中奖号码,但我不知道如何创建上表
import random
def generateRandomNumber():
randomNumber = random.randint(0,30) #can return numbers between 0 and 30 including 0 and 30
return randomNumber#return Random numbers
def drawWinningLotteryNumber():#this function draws the winning numbers
lotteryDrawNumbers = []#list containner for the generated lottery numbers
for currentLotteryNumber in range(8):#for loop to keep generating for 8 counts
randomNumber = generateRandomNumber()# call funtion to generate random numbers
lotteryDrawNumbers.append(randomNumber)# add to the back of the list
return lotteryDrawNumbers#return the list
def printWinningLotteryNumber(lotteryDrawNumbers):#ths function prints the draw numbers
for currentLotteryNumberIndex in range (len(lotteryDrawNumbers)):#for loop to run the print funtion for the length of the list
print(lotteryDrawNumbers[currentLotteryNumberIndex], end = " ")#prints the numbers
def draw():# this funtion brings to gether the draw funtion
lotteryNumbers = drawWinningLotteryNumber()#call function to draw winning numbers
print ("The 8 lottery number for today are :")
printWinningLotteryNumber(lotteryNumbers)#call funtion to print the generated numbers
def generateID():#this function creates the lotto players and their numbers
i = 0
players = []
for currentPlayers in range(100):
i = i + 1
players.append(i)
for currentPlayerIndex in range(len(players)):
print(players[currentPlayerIndex])
generateID()
draw()
可能是这样的:
输出:
我建议使用
random.choices
从可能的数字集合/总体中选择您的随机数。如果您使用random.randint
生成数字,您可能会在给定的抽奖中多次出现相同的数字(据我所知,任何给定编号的彩票球在真正的抽奖中不会出现多次)您可以使用字典
players
将ID与数字列表相关联。试试这个:输出:
相关问题 更多 >
编程相关推荐