首页 > 解决方案 > 创建类的实例时无法传递参数

问题描述

我不明白为什么在尝试创建“Deck”类的实例时,PyCharm 会返回“Deck 不可调用”的错误。

主文件

if __name__ == '__main__':

while play_again is True:
    player_count: int = int(input("Enter number of players: "))
    print(player_count)

    game_deck: Deck = Deck("game") #error on this line
    shuffle(game_deck)
    burn_deck = Deck

甲板.py

class Deck:

card_sequence: Card = []
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']
    self.deck_type = deck_type
    if self.deck_type == "player":
        for i in range(2):
            self.card_sequence = [random.random() for _ in range(2)]
    elif self.deck_type == "game":
        for i in card_suits:
            for j in card_ranks:
                new_card = Card(i, j)
        self.card_sequence.push(new_card)

        for _ in self.card_sequence:
            self.card_sequence.append(random.random())
    elif self.deck_type == "burn":
        self.size = 0
        self.card_sequence = []

错误

Traceback (most recent call last):
  File "C:/Users/Chris/PycharmProjects/BlackJack/Main.py", line 36, in <module>
    game_deck: Deck = Deck("game")
TypeError: 'module' object is not callable

标签: pythonclass

解决方案


您需要导入模块并创建一个对象:

from Deck import Deck
deck = Deck()

或者

import Deck
deck = Deck.Deck()

推荐阅读