|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 IT界文艺代表 于 2022-7-22 17:09 编辑
这个为什么报错。。
def Speech(*words , name):
print(*words + '------' + name)
Speech('hello' , 'see you' , name = 'ABC')
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
Speech('hello' , 'see you' , name = 'ABC')
File "<pyshell#46>", line 2, in Speech
print(*words + '------' + name)
TypeError: can only concatenate tuple (not "str") to tuple
本帖最后由 jackz007 于 2022-7-22 20:48 编辑
函数的收集参数和命名可选参数是这样用的
- def Speech(*words , name = 'ABC') : # 命名可选参数应该出现在函数的定义中,而不是调用时。
- print(',' . join(words) + '------' + name)
复制代码
如果这样调用函数 Speech()
- Speech('hello' , 'see you') # 调用函数时,命名可选参数一般应该省略,但是,如果给出,必须要 "点名道姓"
复制代码
那么,在 Speech() 内,接收到的参数情况是:
- words = ('hello' , 'see you') # 收集参数是一个由所有普通输入参数构成的元组。
- name = 'ABC'
复制代码
元组不可以与字符串相加,但是,如果把字符串作一个新元素被添加进元组是可以的
- words = ('hello' , 'see you')
- name = 'ABC'
- words = words + (name ,) # words = ('hello' , 'see you' , 'ABC')
复制代码
|
|