编辑:我被要求提供整个代码,而不仅仅是相关代码。
我已经阅读了多个类似的错误问题,与此没有任何相似之处。我正在尝试创建一个调用player_build的实例,但是我收到错误“'TotalPlayers'对象没有属性'player_build'”。我想要做的就是让程序显示所有四个玩家:玩家1,玩家2,玩家3,玩家4.我正在使用Atom和Python 3.6.2。
import random
class Card:
def __init__(self, value, suit):
self.value = value
self.suit = suit
def show(self):
if self.value is 11:
print('Jack of {}'.format(self.suit))
elif self.value is 12:
print('Queen of {}'.format(self.suit))
elif self.value is 13:
print('King of {}'.format(self.suit))
elif self.value is 14:
print('Ace of {}'.format(self.suit))
elif self.suit not in ["Spades", "Clubs", "Diamonds", "Hearts"]:
print('Joker')
else:
print('{} of {}'.format(self.value, self.suit))
class Deck:
def __init__(self):
self.card = []
self.build()
def build(self):
for s in ["Joker"]:
for v in range(1, 3):
self.card.append(Card(v, s))
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(2, 15):
self.card.append(Card(v, s))
def show(self):
for c in self.card:
c.show()
def shuffle(self):
for i in range(len(self.card) -1, 0, -1):
random.shuffle(self.card)
def draw(self):
return self.card.pop()
class Player:
def __init__(self, player, number):
self.player = player
self.number = number
def show(self):
print('{} {}'.format(self.player, self.number))
class TotalPlayers:
def __init__(self):
self.starting_players = []
self.player_build()
def player_build(self):
for p in ["Player"]:
for n in range(1, 5):
self.starting_players.append(Player(p, n))
def show(self):
for s in self.starting_players:
s.show()
我试着这样运行代码:
m = TotalPlayers()
m.show()
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\jmcco\Desktop\Gambit Card Game\Gambit_Main.py", line 72,
in <module>
m = TotalPlayers()
File "C:\Users\jmcco\Desktop\Gambit Card Game\Gambit_Main.py", line 57, in
__init__
self.player_build()
AttributeError: 'TotalPlayers' object has no attribute 'player_build'