鱼C论坛

 找回密码
 立即注册
查看: 2264|回复: 4

Python 小白问题传递列表中的返回值

[复制链接]
发表于 2020-3-27 10:28:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 在下小丁 于 2020-3-27 10:29 编辑

def greet_users(names):
  for name in names:
   msg="hello "+name.title()+" !"
   print(msg)
usernames=['mayun','xiaomage','wangjianlin']
greet_users(usernames)


怎样将上面这种列表传递改成下面用返回值表示的形式

def greet_users(names):

  for name in names:
    msg=name.title()
    return msg     #这里的返回函数值
usernames=['mayun','xiaomage','wangjianlin']
say=greet_users(usernames)
print(say)
下面这种只能输出 Mayun 后面的输出不了
小白在线求助 求求各位了  看了好久了 还是弄不好



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-27 10:31:55 | 显示全部楼层
你不会学了函数连return都没学会吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 10:38:51 | 显示全部楼层
def greet_users(names):
    msg=[]
    for name in names:
        msg.append("hello "+name+" !")
    return msg
usernames=['mayun','xiaomage','wangjianlin']
say=greet_users(usernames)
for words in say:
    print(words)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 11:05:37 | 显示全部楼层
函数要么不返回值,返回的话返回值唯一,你那return在for循环内,它就只返回第一次的return
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 11:17:40 | 显示全部楼层
def greet_users(names):
    msg = 'hello ' + names + ' !'
    return msg
usernames = ['mayun','xiaomage','MM']
for i in range(len(usernames)):
    print(greet_users(usernames[i]))

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-3-15 05:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表