Python 3:列表索引超出范围
每次我运行这个命令时,我都无法通过所有的卡而不出现这个错误<代码>索引器错误:列表索引超出范围Python 3:列表索引超出范围,python,list,python-3.x,Python,List,Python 3.x,每次我运行这个命令时,我都无法通过所有的卡而不出现这个错误索引器错误:列表索引超出范围 import random cards = ['2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', ',10', '10', '10
import random
cards = ['2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', ',10', '10', '10', 'J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A']
randomness = 51
while True:
cardIndex = random.randint(0, randomness)
del cards[cardIndex]
randomness = randomness -1
print(cards[cardIndex])
在按该索引删除之前,打印卡片[cardIndex]:
while cards: # Because we need to stop somewhere
cardIndex = random.randint(0, randomness)
print(cards[cardIndex])
del cards[cardIndex]
randomness = randomness -1
您根本不需要随机性
:
while cards:
cardIndex = random.randrange(len(cards))
print(cards[cardIndex])
del cards[cardIndex]
您可以使用以下两种方法之一: 或(将修改
卡
列表):
在按该索引删除之前,打印卡片[cardIndex]:
while cards: # Because we need to stop somewhere
cardIndex = random.randint(0, randomness)
print(cards[cardIndex])
del cards[cardIndex]
randomness = randomness -1
您根本不需要随机性
:
while cards:
cardIndex = random.randrange(len(cards))
print(cards[cardIndex])
del cards[cardIndex]
您可以使用以下两种方法之一: 或(将修改
卡
列表):
您的问题是,您首先删除该卡,然后打印它,因此问题在于您选择最后一张卡时:
randomness = 51
while True:
cardIndex = random.randint(0, randomness) #random.randint(0, 51) could give num 51 so card 52
del cards[cardIndex] # deleted 52nd card (num 51)
randomness = randomness -1
print(cards[cardIndex]) # there is no 52nd card (num 51) anymore
您要更改的是打印和删除:
while len(cards) > 0: # once yours pack of card is empty you want to stop
cardIndex = random.randint(0, randomness)
print(cards[cardIndex])
del cards[cardIndex]
randomness = randomness -1
告诉你们清单上有多少东西
它现在应该可以工作了:)您的问题是先删除卡片,然后再打印,所以问题是当您选择最后一张卡片时:
randomness = 51
while True:
cardIndex = random.randint(0, randomness) #random.randint(0, 51) could give num 51 so card 52
del cards[cardIndex] # deleted 52nd card (num 51)
randomness = randomness -1
print(cards[cardIndex]) # there is no 52nd card (num 51) anymore
您要更改的是打印和删除:
while len(cards) > 0: # once yours pack of card is empty you want to stop
cardIndex = random.randint(0, randomness)
print(cards[cardIndex])
del cards[cardIndex]
randomness = randomness -1
告诉你们清单上有多少东西
它现在应该可以工作了:)所以基本上是洗一副牌?另外,你有
,,,,,,,,
,所以你的10不是10,而是“逗号10”。是的,我想洗牌一副牌,分发每一张牌。逗号不会改变任何东西…以随机顺序打印卡片:random.shuffle(卡片);打印(“\n”.join(cards))
如果下面的一个答案解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道你的问题已经解决到了令你满意的程度,并且它给了帮助你的人帮助的积分。那么基本上洗一副牌?另外,你有,,,,,,,,
,所以你的10不是10,而是“逗号10”。是的,我想洗牌一副牌,分发每一张牌。逗号不会改变任何东西…以随机顺序打印卡片:random.shuffle(卡片);打印(“\n”.join(cards))
如果下面的一个答案解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道你的问题已经解决到了令你满意的程度,并让帮助你的人相信你的帮助。还有random.shuffle(seq),它将所有问题都随机地处理到位。[它甚至让它听起来像一副牌!]还有random.shuffle(seq),它将所有的牌随机地洗牌到位。[它甚至听起来像一副牌!]我更改了你让我更改的内容,但在分发所有“牌”之前出现错误。我更改了你让我更改的内容,但在分发所有“牌”之前出现错误。谢谢!你发现我的剧本有什么问题!!它现在工作得很好,你向我解释的方式真的让我明白了为什么会发生这个错误!谢谢你发现我的剧本有什么问题!!它现在工作得很好,你向我解释的方式真的让我明白了为什么会发生这个错误!