如何解决此错误:AttributeError:'list'对象没有属性'items'?

时间:2019-04-12 22:28:31

标签: python attributeerror

我正在尝试添加从卡组中拉出的卡的值。我不知道如何打印卡的附加值。

我尝试更改模块并以其他方式导入模块。

useofhand.py

def main():
    suits = ['Spades', 'Diamonds', 'Hearts', 'Clubs']
    ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] 
    score = ScoredHand()
    thedeck = frenchdeck(ranks, suits)

createdeck.py

def frenchdeck(ranks, suits):
    decklist = []

    for rank in ranks:
        for suit in suits:
            decklist.append(ScoredCard(rank, suit, ranks, suits))

    return Deck(decklist)

scoredcard.py

class ScoredCard(Card):

    def __init__(self, rank, suit, rank_encoding = None, suit_encoding = None, score_encoding = None):

        super().__init__(rank, suit, rank_encoding, suit_encoding)

        self._score_encoding = score_encoding

card.py

def reversedict(dictionary):
    return {v:k for k, v in dictionary.items()}

class Card:

    def __init__(self, rank, suit, rank_encoding, suit_encoding):
        self.setrank(rank)
        self.setsuit(suit)
        self._rank_decoding = reversedict(rank_encoding)
        self._rank_encoding = rank_encoding
        self._suit_decoding = reversedict(suit_encoding)
        self._suit_encoding = suit_encoding

输出

Traceback (most recent call last):
  File ".\useofhand.py", line 30, in <module>
    main()
  File ".\useofhand.py", line 11, in main
    thedeck = frenchdeck(ranks, suits)
  File "C:\Users\MINE\Documents\cisc_120_files\lab4\createdeck.py", line 10, in frenchdeck
    decklist.append(ScoredCard(rank, suit, ranks, suits))
  File "C:\Users\MINE\Documents\cisc_120_files\lab4\scoredcard.py", line 7, in __init__
    super().__init__(rank, suit, rank_encoding, suit_encoding)
  File "C:\Users\MINE\Documents\cisc_120_files\lab4\card.py", line 9, in __init__
    self._rank_decoding = reversedict(rank_encoding)
  File "C:\Users\MINE\Documents\cisc_120_files\lab4\card.py", line 2, in reversedict
    return {v:k for k, v in dictionary.items()}
AttributeError: 'list' object has no attribute 'items'

1 个答案:

答案 0 :(得分:0)

您的ranks是一个列表,并且变成rank_encoding是您的代码错误所在。您正在运行:

self._rank_decoding = reversedict(rank_encoding)

正在这样做:

return {v:k for k, v in rank_encoding.items()}

同时:

rank_encoding = ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']

我认为您想做的是:

return rank_encoding.reverse()