注册 登录
编程论坛 Python论坛

请大神帮忙,应该return 什么,关于这个函数还有没有更好的编法???

dawn曙光 发布于 2016-04-04 16:53, 5291 次点击
def y():
    for x in range(1,z+1):                                  #x:the number what you want
        m=str(input("string %i:"%x))
        n.append(m)
    return n
n=[]
z=int(input("How many items do you want in your list ?"))
print(y())


def p():
    total=0
    for q in n:
        print("The length of the string %s is :%i"%(p,len(q)))
        total += len(q)
    return
print(p())
1 回复
#2
lonmaor2016-04-06 10:00
你不是要return total吗?返回所有字符串的长的和。
n是个全局变量,python3自定义函数内部不用声明global n吗?
你既然声明了全局变量n,且在y()和p()中都对n进行了操作,那么y()中return n就是多此一举。
不过,没有返回值的函数不是好函数。
1