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()
当我运行这段代码时,它会拒绝“def a(...”并用红色突出显示“(”。我不知道为什么。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()
当我运行这段代码时,它会拒绝“def a(...”并用红色突出显示“(”。我不知道为什么。让我在这里澄清两点:
(a='b', c)
。
函数中定义参数的正确顺序是:
(a, b, c)
(a='b', r='j')
(*args)
(**kwargs)
def example(a, b, c=None, r="w", d=[], *ae, **ab):
(a,b)
是位置参数
(c=none)
是可选参数
(r="w")
是关键字参数
(d=[])
是列表参数
(*ae)
是仅限关键字参数
(*ab)
是可变关键字参数
所以首先重新排列你的参数
因此,第二步需要删除这个代码"len1 = hgt"
,因为它在Python中是不允许的。
记住参数和参数之间的区别。
错误信息提示,非默认参数til
不应在默认参数hgt
之后。
更改参数顺序(函数调用也需要相应调整)或将hgt
设置为非默认参数即可解决问题。
def a(len1, hgt=len1, til, col=0):
->
def a(len1, hgt, til, col=0):
更新
SyntaxError
掩盖了另一个问题。
os.system
仅接受一个字符串参数。
def a(len1, hgt, til, col=0):
system('mode con cols=%s lines=%s' % (len1, hgt))
system('title %s' % til)
system('color %s' % col)
在关键字参数后面不能有非关键字参数。
请确保重新排列您的函数参数,以此方式:
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")
在函数内部,所有的变量都应该以相同的格式进行定义。虽然你给一些变量设置了默认值。
def(len1, hgt=len1, til, col=0):
应该是这样的
def(len1, hgt, til, col):
或者
def(len1=value, hgt=value, til=value, col=value):