列表包含浮点数,而不包含预期的对象

时间:2018-07-31 01:01:13

标签: python oop

我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/Chris/PycharmProjects/BlackJack/Main.py", line 50, in <module>
    player = Player("Player" + str(i))
  File "C:\Users\Chris\PycharmProjects\BlackJack\Player.py", line 14, in __init__
    self.score = self.calculate_hand(self.hand)
  File "C:\Users\Chris\PycharmProjects\BlackJack\Player.py", line 21, in calculate_hand
    rank = i.get_card_value()
AttributeError: 'float' object has no attribute 'get_card_value'

问题是我正在访问的索引被识别为“浮点数”,但列表中不应包含浮点数,而应包含“ Card”对象。更让我困惑的是在PyCharm IDE中,当我编写“ index”时。要访问属性,建议将“ get_card_value”作为可使用的属性(函数)。这与错误有关,该错误说索引(似乎是一个浮点数)没有属性“ get_card_value”。


Main.py

player = Player("Player" + str(i))

Player.py

职业玩家:     名称:字符串     手:甲板     得分:整数= 0

def __init__(self, name: string):
    self.name = name
    self.hand = Deck("player")
    self.score = self.calculate_hand(self.hand)

@staticmethod
def calculate_hand(hand):
    cards = hand.get_deck()
    value: int = 0
    for i in cards:
        rank = i.get_card_value()
        value = value + rank
    return value

Card.py

class Card:
suit: string
rank: string
card_suits: string = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
card_ranks: string = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Jack', 'Queen', 'King']

def __init__(self, suit: string, rank: string):
    self.suit = suit
    self.rank = rank

def get_card(self):
    return [self.rank, self.suit]

def get_rank(self):
    return self.rank

def get_suit(self):
    return self.suit

def get_card_value(self):
    rank = self.get_rank()
    value: int = self.card_ranks.index(rank) + 1
    return value

Deck.py

import random
import string
from Card import Card
import Deck

class Deck:

card_sequence = []
deck_type = ""

def __init__(self, deck_type):
    card_suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    card_ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Jack', 'Queen', 'King']
    card_sequence = []
    for i in card_suits:
        for j in card_ranks:
            new_card = Card(i, j)
            card_sequence.append(new_card)
    self.deck_type = deck_type
    if self.deck_type == "player":
        for _ in range(2):
            self.card_sequence.append(random.choice(card_sequence))

    elif self.deck_type == "game":
        for i in card_sequence:
            self.card_sequence.append(i)

    elif self.deck_type == "burn":
        self.size = 0
        self.card_sequence = []

def draw_card(self, deck: Deck):
    draw = deck.get_top_card()
    deck.pop(draw)
    deck.size = deck.size - 1
    self.card_sequence.append(draw)

def get_deck(self):
    return self.card_sequence

def get_item(self, card_sequence_index):
    if card_sequence_index > self.size - 1:
        print("ERROR item doesn't exist")
        return

def get_top_card(self):
    item: int = self.card_sequence[0]
    return item

def get_card(self, deck):
    card = deck.get_front_item()
    self.card_sequence.push(card)
    card.pop()
    return card

def add_card(self, item):
    if item is None:
        self.card_sequence.push(item)

def remove_card(self):
    self.card_sequence.pop()

0 个答案:

没有答案