96
from os import system
def a(len1,hgt=len1,til,col=0):
    system('mode con cols='+len1,'lines='+hgt)
    system('title',til)
    system('color',col)

a(64,25,"hi","0b")
input()

When I run this, it rejects "def a(..." and highlights "(" in red. I have no clue why.

4

4 Answers 4

168

Let me clarify two points here:

  • Firstly non-default argument should not follow the default argument, it means you can't define (a='b', c) in function. The correct order of defining parameter in function are:
    • positional parameter or non-default parameter i.e (a, b, c)
    • keyword parameter or default parameter i.e (a='b', r='j')
    • keyword-only parameter i.e (*args)
    • var-keyword parameter i.e (**kwargs)
def example(a, b, c=None, r="w", d=[], *ae,  **ab):

(a,b) are positional parameter

(c=none) is optional parameter

(r="w") is keyword parameter

(d=[]) is list parameter

(*ae) is keyword-only

(*ab) is var-keyword parameter

so first re-arrange your parameters

  • now the second thing is you have to define len1 when you are doing hgt=len1 the len1 argument is not defined when default values are saved, Python computes and saves default values when you define the function len1 is not defined, does not exist when this happens (it exists only when the function is executed)

so second remove this "len1 = hgt" it's not allowed in python.

keep in mind the difference between argument and parameters.

1
  • 13
    Don't use an empty list as a default value for a function parameter, it gets carried across runs. Use None instead.
    – Josepas
    Commented Apr 30, 2019 at 15:28
20

As the error message says, non-default argument til should not follow default argument hgt.

Changing order of parameters (function call also be adjusted accordingly) or making hgt non-default parameter will solve your problem.

def a(len1, hgt=len1, til, col=0):

->

def a(len1, hgt, til, col=0):

UPDATE

Another issue that is hidden by the SyntaxError.

os.system accepts only one string parameter.

def a(len1, hgt, til, col=0):
    system('mode con cols=%s lines=%s' % (len1, hgt))
    system('title %s' % til)
    system('color %s' % col)
2
  • THANK YOU. One more thing, could you please give me a link to any youtube Python tutorial series THAT ISN'T OUT OF DATE? plz? Commented Aug 22, 2014 at 23:10
  • @AidanCodeX, I don't know about youtube Python tutorial. How about Python tutorial from python.org ?
    – falsetru
    Commented Aug 23, 2014 at 2:30
15

You can't have a non-keyword argument after a keyword argument.

Make sure you re-arrange your function arguments like so:

def a(len1,til,hgt=len1,col=0):
    system('mode con cols='+len1,'lines='+hgt)
    system('title',til)
    system('color',col)

a(64,"hi",25,"0b")
1
  • 1
    Best simple and helpful answer for me. I think Python should fix this.
    – Sos.
    Commented May 24, 2020 at 9:52
0

Inside the function all the variables should be defined in the same format. While you have given default values to some variables.

def(len1, hgt=len1, til, col=0):

It should be like

def(len1, hgt, til, col):

or

def(len1=value, hgt=value, til=value, col=value):

Not the answer you're looking for? Browse other questions tagged or ask your own question.