python default argument syntax error

2024/11/16 12:42:30

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().centerxelse:self.textRect.x = x_locationif y_location == "center":self.textRect.centery = screen.get_rect().centeryelse:self.textRect.y = y_locationself.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?

Answer
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://en.xdnf.cn/q/119531.html

Related Q&A

Variable not defined (Python)

FlightType=input("Which flight would you like to fly? Type 2 Seater, 4 Seater, or Historic.") # No validation included for the inputFlightLen=input("Would you like to book the 30 minu…

PyGame: draw.rect() has invalid parameters

Im trying to learn mouse events with PyGame, and Im trying to draw a box wherever the user clicks. Im setting a variable equal to pygame.mouse.get_pos(), and calling individual tuple members according …

Cant press enter in selenium2library

Im trying to make a test that will open Facebook, log in and search something. However Im having trouble getting Facebook to search. Selenium types whatever it needs in the search bar, but I cant find …

Converting string to datetime in Python using strptime

Im trying to convert the following String to datetime object in Python.datetime_object = datetime.strptime(Sat, 26 Nov 2016 15:17:00 +0000, %a, %b %d %Y %H:%c %z)I get the following error,File "&l…

Is there any differences between python2 and python3 about adding menu bar to Frame in tkinter?

Im trying to porting a project 2to3 on python, and stuck in tkinter.In python2, there is no problem with adding menu bar to Frame in tkinter,but python3 occured attribute error. (Frame object has no at…

Standard Input having weird characters with them in different programming lanuage

I am getting confused with the standard input of these programming languages : [Note:] I added details about so many programming languages as the problem with all of them is same in this matter and my …

How to turn a numpy array to a numpy object?

I have a NumPy array as follows: [[[ 0 0]][[ 0 479]][[639 479]][[639 0]]]and I would like to convert it into something like so: [( 0 0)( 0 479)(639 479)(639 0), dtype=dtype([(x, <i2), (y…

recover all line from an attribute in a database in json

To simplify my problem, I have a base in json, and I recover all of my lines of json to put informations in a base. It seems easy for moments, but problem is that my json is not correctly writtenSo i…

Calculate eigen value in python as same way(order) in Matlab

This is the Matlab code which is returning eigenvector in V and eigenvalue in D. Consider C is 9*9 matrix then V is 9*9 matrix and D is 9*9 diagonal. matrix.[V,D] = eig(C);I want the same thing in Pyth…

Python: ctypes and Pointer to Structure

I am trying to make a pointer of a struct and then de-reference it. But its crashing. I have mimiced the behvior here with this simple code. from ctypes import * import ctypesclass File(Structure):_fie…