IndexError:Python 3中的列表索引超出范围

夏天

我是Python的新手。运行代码时出现索引错误。我已经在Stackoverflow中看到了相关的问题,但是我仍然看不到错误是什么。我非常感谢您的任何回应。谢谢你。这是代码:

 class Card:
   def __init__(self, suit = 0, rank = 2):
       self.suit = suit
       self.rank = rank

   suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
   rank_names =[None,'Ace','2','3','4','5','6','7','9','10','Jack','Queen', 'King']

   def __str__ (self):
       return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])

   def __lt__(self,other):
       t1 = self.suit, self.rank
       t2 = other.suit, other.rank
       return t1 < t2


class Deck:
   def __init__(self):
       self.cards = []
       for suit in range(4):
           for rank in range(1, 14):
               card = Card(suit, rank)
               self.cards.append(card)

   def __str__(self):
       res = [ ]
       for card in self.cards:
           res.append(str(card))
       return '\n'.join(res)


deck1 = Deck()
print(deck1)

然后我得到以下错误:

   Traceback (most recent call last):
  File "/Users/Enze/Python/untitled/Inheritance.py", line 35, in <module>
    print(deck1)
  File "/Users/Enze/Python/untitled/Inheritance.py", line 30, in __str__
    res.append(str(card))
  File "/Users/Enze/Python/untitled/Inheritance.py", line 11, in __str__
    return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])
IndexError: list index out of range
安科维奇

13rank_names列表中项目,因此最后一个元素的索引很有价值12(列表以开头0)-内部循环

for suit in range(4):
       for rank in range(1, 14):
           card = Card(suit, rank)

您尝试获取的最大索引是13range正在按顺序逐个生成数字不包括边框)),这就是为什么出现index out of range异常的原因

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python列表中的IndexError-“列表索引超出范围”

来自分类Dev

python 3 IndexError:列表索引超出范围

来自分类Dev

Python 3:IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围(Python 3)

来自分类Dev

IndexError:在python列表中列出索引超出范围

来自分类Dev

获取IndexError:在python中列表分配索引超出范围

来自分类Dev

python中的“IndexError:列表索引超出范围”错误

来自分类Dev

Python,列表错误:IndexError:列表索引超出范围

来自分类Dev

Python IndexError:列表分配索引超出范围

来自分类Dev

python错误:IndexError:列表索引超出范围

来自分类Dev

IndexError:列表分配索引超出范围,Python

来自分类Dev

Python 3.7.2:IndexError:列表索引超出范围

来自分类Dev

Python CSV:IndexError:列表索引超出范围

来自分类Dev

Python Web抓取“ IndexError:列表索引超出范围”

来自分类Dev

IndexError:列表索引超出范围Python参数输入

来自分类Dev

Python-IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围(Python 2.7)

来自分类Dev

Python IndexError:列表索引超出范围PushBullet

来自分类Dev

IndexError:列表分配索引超出范围-带数组的Python

来自分类Dev

python xpath IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围错误python

来自分类Dev

Python:(IndexError:列表索引超出范围)readFile

来自分类Dev

IndexError:列表索引超出范围 - Python 3.5.2

来自分类Dev

Python - IndexError:列表索引超出范围 - 不工作

来自分类Dev

Python 单元测试 - IndexError:列表索引超出范围

来自分类Dev

IndexError:数组搜索中的列表索引超出范围

来自分类Dev

IndexError:数组反转中的列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围

来自分类Dev

Yfinance IndexError:列表索引超出范围

Related 相关文章

  1. 1

    python列表中的IndexError-“列表索引超出范围”

  2. 2

    python 3 IndexError:列表索引超出范围

  3. 3

    Python 3:IndexError:列表索引超出范围

  4. 4

    IndexError:列表索引超出范围(Python 3)

  5. 5

    IndexError:在python列表中列出索引超出范围

  6. 6

    获取IndexError:在python中列表分配索引超出范围

  7. 7

    python中的“IndexError:列表索引超出范围”错误

  8. 8

    Python,列表错误:IndexError:列表索引超出范围

  9. 9

    Python IndexError:列表分配索引超出范围

  10. 10

    python错误:IndexError:列表索引超出范围

  11. 11

    IndexError:列表分配索引超出范围,Python

  12. 12

    Python 3.7.2:IndexError:列表索引超出范围

  13. 13

    Python CSV:IndexError:列表索引超出范围

  14. 14

    Python Web抓取“ IndexError:列表索引超出范围”

  15. 15

    IndexError:列表索引超出范围Python参数输入

  16. 16

    Python-IndexError:列表索引超出范围

  17. 17

    IndexError:列表索引超出范围(Python 2.7)

  18. 18

    Python IndexError:列表索引超出范围PushBullet

  19. 19

    IndexError:列表分配索引超出范围-带数组的Python

  20. 20

    python xpath IndexError:列表索引超出范围

  21. 21

    IndexError:列表索引超出范围错误python

  22. 22

    Python:(IndexError:列表索引超出范围)readFile

  23. 23

    IndexError:列表索引超出范围 - Python 3.5.2

  24. 24

    Python - IndexError:列表索引超出范围 - 不工作

  25. 25

    Python 单元测试 - IndexError:列表索引超出范围

  26. 26

    IndexError:数组搜索中的列表索引超出范围

  27. 27

    IndexError:数组反转中的列表索引超出范围

  28. 28

    IndexError:列表索引超出范围

  29. 29

    Yfinance IndexError:列表索引超出范围

热门标签

归档