简体   繁体   English

Python列表索引超出范围列表

[英]Python List Index Out of Range Lists

I am new to programming and was creating a portion of a program for this project: http://www.reddit.com/r/beginnerprojects/comments/19ot36/project_a_variation_of_21/ when I ran into a index error. 我是编程新手,正在为这个项目创建一个程序的一部分: http//www.reddit.com/r/beginnerprojects/comments/19ot36/project_a_variation_of_21/当我遇到索引错误时。 Using Stackoverflow answers, I originally corrected the error by making 使用Stackoverflow答案,我最初通过制作纠正错误

random.randrange(0,len(cards)+1)]

instead of 代替

random.randrange(0,53,1)]

However it continues to give me this error. 但是它继续给我这个错误。 If you run it 50 times it might not give you an error, but it might give you an error the first or fifth time you run it too. 如果你运行50次它可能不会给你一个错误,但它也可能在你第一次或第五次运行时给你一个错误。 For the round function, I want to be able to deal cards from a single deck, each time I draw a card it removes it from the deck. 对于圆形功能,我希望能够从单个牌组中处理牌,每次我拿出一张卡牌将其从牌组中移除。 Any advice would be most appreciated! 任何建议将非常感谢! - Thomas - 托马斯

Error Message: 错误信息:

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module2>", line 39, in <module>
  File "<module2>", line 30, in round
  File "<module2>", line 26, in draw

IndexError: list index out of range IndexError:列表索引超出范围

Code: 码:

def round():
    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", "Jack", "Jack", "Jack", "Jack",
    "Queen", "Queen", "Queen", "Queen", "King", "King", "King", "King", "Ace", "Ace",
    "Ace", "Ace"]
    def draw():
        return cards[random.randrange(0,len(cards)+1,1)]
    acards = []
    aroundscore = 0
    acards.append(draw())
    acards.append(draw())
    print(acards)
    print(acards)
    cardsvalues = {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9,
    "10":10, "Jack":10, "Queen":10, "King":10, "Ace":1}
    for i in acards:
        print(cardsvalues[i])

The reason you use len(cards) is because the amount of cards could change if you deal a card! 你使用len(cards)的原因是因为如果你交易卡,卡的数量可能会改变! your line should be len(cards) not len(cards) + 1 because if you add one to the maximum length of cards and it can then randomly pick a number that will be larger than the amount of cards you have, thereby causing an IndexError 你的行应该是len(cards)而不是len(cards) + 1因为如果你在cards的最大长度上添加一个,那么它可以随机选择一个大于你所拥有的卡片数量的数字,从而导致一个IndexError

The error is occurring in your draw function because your range is too large: random.randrange(0,len(cards)+1,1) . 您的draw函数中发生错误,因为您的范围太大: random.randrange(0,len(cards)+1,1) Think about, for example if len(cards) was 3. Then, the rangerange would be called with 0, 4, 1 , which means that it will return integers between 0 and 3. But the index 3 wouldn't exist - it's an off-by-one error. 想想看,例如,如果len(cards)为3。然后, rangerange将与被称为0, 4, 1 ,这意味着它将0和3。但是指数之间返回整数3也就不存在了-这是一个一个错误。

I'd recommend just using random.choice to accomplish what you want. 我建议只使用random.choice来完成你想要的。 It's much more concise and readable. 它更简洁,更易读。

def draw():
    return random.choice(cards)

使用random.randit(0,51)我认为你对卡的长度感到困惑。

I agree with above suggestions. 我同意上述建议。 But here is one more thing: randrange does not guarantee that generated numbers will be different. 但是还有一件事:randrange并不保证生成的数字会有所不同。 You can get a few numbers of the same value. 您可以获得一些相同值的数字。 It is very bad for card game - because every card in a game must be unique. 对于纸牌游戏来说非常糟糕 - 因为游戏中的每张牌都必须是唯一的。 I'd suggest to use random.shuffle and take cards from the list one by one from the beginning. 我建议使用random.shuffle并从头开始逐个从名单中取出卡片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM