gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:31 28 4
gpt4 key购买 nike

我不明白为什么在尝试创建“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()

关于python - 创建类的实例时无法传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51576590/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com