使用ID进行2D阵列数据检索



基本上,我正在创建一个小型彩票程序,我想做一个功能,用户可以输入他/她的id,它会给出他们的号码,并让他们知道自己是否是中奖

玩家号码存储在一个2D数组**(而不是数字(**中,就像这个[[2, 17, 19, 19, 21, 29, 8, 17],[9, 5, 17, 18, 23, 28, 2, 2]]一样,所以如果玩家输入了他们的id as 1,那么它应该完全返回第二个数组,因为我想检查那个玩家是否是赢家。

我已经完成了以下操作,但它不起作用,当我单独运行时,玩家检查部分确实起作用

IndexError: list index out of range

player = [[2, 17, 19, 19, 21, 29, 8, 17],[9, 5, 17, 18, 23, 28, 2, 2],[5, 8, 18, 18, 29, 30, 25, 26],[5, 6, 15, 13, 23, 24, 12, 12]]
win = [2, 17, 19, 19, 21, 29, 8, 17]
group1 = set(win[0:6])
group2 = set(win[6:8])
playerID = int(input("Please enter player ID "))
print(playerID)
print(player[playerID])
for i in player[playerID]:
count = 0
for j in range(6):
x = player[i][j]
if x in group1:
count+=1
print("score")
if j == 5:
if count == 6:
print("You are a winner")
elif count == 5:
print("You win with 5 numbers" )
elif count == 4:
print("You win with 4 numbers")
elif count == 3:
print(  "you win with 3 numbers")
elif count < 3:              
count2 = 0
for l in range(6,8):
y = player[i][l]
if y in group2:
print("you win with bouns numbers")
else: 
print("sorry you lose")


你一开始让我很困惑。您使用的不是数组,而是列表列表。数组是python中特定的东西(来自数组模块(,并且总是1D。

话虽如此,您的代码(使用列表列表(是正确的。只有当用户键入并且ID>3.

最新更新