尽管似乎有几个人回答了这个问题,但是他们经常提到继承。运行此段时,收到错误消息:TypeError: __init__() takes 1 positional argument but 2 were given
:
class game:
def __init__(self):
global drawPile
self.x = 1
drawPile = deck(playerCount)
此代码:
# Modules #
import random, time, termcolor
from os import system
# Functions ##
def clear(wait=0):
time.sleep(wait)
_ = system("clear")
"""
def cp(string, color=None, on_color=None, attrs=None):
cprint(termcolor.colored(string,color,on_color,attrs))
"""
def cprint(string, color= "magenta"):
print(termcolor.colored(string, color))
def ans(x = ">>| ", color = "cyan"):
return input(termcolor.colored(x, color))
cprint("How many players are there")
playerCount = int(ans())
class deck:
def __init__(self):
global player1, player2, player3, player4, player5, player6, playerlist
if playerCount == 2:
embasy = 0
player1 = player(1)
player2 = player(2)
playerlist = [player1, player2]
elif playerCount == 3:
embasy = 0
player1 = player(1)
player2 = player(2)
player3 = player(3)
playerlist = [player1, player2, player3]
elif playerCount == 4:
embasy = 1
player1 = player(1)
player2 = player(2)
player3 = player(3)
player4 = player(4)
playerlist = [player1, player2, player3, player4]
elif playerCount == 5:
embasy = 2
player1 = player(1)
player2 = player(2)
player3 = player(3)
player4 = player(4)
player5 = player(5)
playerlist = [player1, player2, player3, player4, player5]
elif playerCount == 6:
embasy = 3
player1 = player(1)
player2 = player(2)
player3 = player(3)
player4 = player(4)
player5 = player(5)
player6 = player(6)
playerlist = [player1, player2, player3, player4, player5, player6]
playerlist = playerlist
self.set = [["embasy"] * embasy, ["diplomat (-1)"]*10, ["master spy (-5)"] * 10, ["celebrity 5"] * 10, ["investigate"] * 10, ["embargo"] * 10, ["safe house"] *10, ["embargo"] * 10, ["counterintellegence"] * 10, ["jail cell"] * 10, ["college student (1)"] * 10, ["family (3)"] * 10, ["spy (-3)"] * 10]
self.cards = []
for i in self.set:
for j in i:
self.cards.append(j)
self.cards = random.sample(self.cards, len(self.cards))
def deal(self):
for j in range(5):
for i in playerlist:
i.drawCard(self.cards.pop(0))
class player:
def __init__(self, number):
embasy = [False, []]
self.otherPlayers = []
for i in range(1, start.drawPile.playerCount + 1):
self.otherPlayers.append(number)
self.otherPlayers.remove(number)
self.hand = []
self.leftPile = []
self.rightPile = []
self.embasyPile = []
self.jailPiles = [0, []]
def drawCard(self, card):
self.hand.append(card)
def playCard(self, card):
self.hand.remove(card)
def recieveCard(self, card, pile):
if pile == "left":
self.leftPile.append(card)
elif pile == "right":
self.leftPile.append(card)
elif pile == "embasy":
self.leftPile.append(card)
elif pile == "jail":
self.jailPiles[2].append(card)
def playTurn(self):
clear(3)
cprint("In your hand you have:")
for i in self.hand:
cprint(f"\t{i}")
time.sleep(2)
cprint("Which would you like to play?")
card = ans()
if card not in self.hand:
self.playTurn()
if card == "embasy":
pass
class game:
def __init__(self):
global drawPile
self.x = 1
drawPile = deck(playerCount)
start = game()
如果我对程序的大部分内容表示歉意。
答案 0 :(得分:2)
当您调用deck构造函数时,您将尝试在此行中传递参数:
drawPile = deck(playerCount)
但是您将deck构造函数声明为不带参数,除了self:
class deck:
def __init__(self):
这就是Python所抱怨的,但是错误消息的实际文本使初学者感到困惑。由于deck是类而不是函数,因此Python自动向构造函数调用添加第一个参数self。这就是为什么错误消息显示“接受1个位置参数,但给出2个”的原因。错误消息将“ self”作为一个参数。由于您尝试提供另一个,所以意味着“给出了两个。”
答案 1 :(得分:1)
您的playerCount
变量是全局变量,因此在创建deck
对象时无需将其作为参数传递。因此,只需将以下行替换类game
:
class game:
def __init__(self):
global drawPile
self.x = 1
drawPile = deck()