python default argument syntax error

不问归期 提交于 2020-01-07 09:03:23

问题


I just wrote a small text class in python for a game written using pygame and for some reason my default arguments aren't working. I tried looking at the python documentation to see if that might give me an idea what I did wrong, but I didn't fully get the language in the documentation. No one else seems to be having this issue.

Here's the code for the text class:

class Text:
    def __init__(self,screen,x_location='center',y_location='center',writeable,color,size=12):
        self.font = pygame.font.Font("Milleni Gem.ttf", size)

        self.text = self.font.render(writeable,True,color)
        self.textRect = self.text.get_rect()

        if x_location == "center":
            self.textRect.centerx = screen.get_rect().centerx
        else:
            self.textRect.x = x_location

        if y_location == "center":
            self.textRect.centery = screen.get_rect().centery
        else:
            self.textRect.y = y_location
        self.update()

    def update(self):
        screen.blit(self.text,self.textRect)

and here's the code to call it:

from gui import *
Text(screen,75,75,currentweapon.clip)#currentweapon.clip is an integer

The error I get is this:

SyntaxError: non-default argument follows default argument

and points to the def __init__() line in the code. What does this error mean and what am I doing wrong here?


回答1:


def __init__(self,screen,x_location='center',y_location='center',writeable,color,size=12):

You have defined non-default arguments after default arguments, this is not allowed. You should use:

def __init__(self,screen,writeable,color,x_location='center',y_location='center',size=12):


来源:https://stackoverflow.com/questions/17893820/python-default-argument-syntax-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!