|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def fun1(str1):
global list1
list1= []
for each in str1:
list1 = list1.append(each)
return list1
str1 = 'iukjn 320.'
fun1(str1)
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
fun1(str1)
File "<pyshell#6>", line 5, in fun1
list1 = list1.append(each)
AttributeError: 'NoneType' object has no attribute 'append'
你代码没必要用 global,因为最后函数返回了 list1 了,而且你在开始时候初始化了 list1,参考代码:
- def fun1(str1):
- list1= []
- for each in str1:
- list1.append(each)
- return list1
- str1 = 'iukjn 320.'
- print(fun1(str1))
复制代码
可以直接对字符串使用 list 函数,就直接变为列表了,参考代码:
- def fun1(str1):
- return list(str1)
- str1 = 'iukjn 320.'
- print(fun1(str1))
复制代码
|
|