Skip to main content
 首页 » 编程设计

python之创建类的实例时无法传递参数

2024年12月31日9luoye11

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

主.py

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 

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 

请您参考如下方法:

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

from Deck import Deck 
deck = Deck() 

或

import Deck 
deck = Deck.Deck()