Print和return
跳转到导航
跳转到搜索
print on ipython
In [38]: def printh(name,sex):
...: sex_dict = {1:u'先生',2: u'女士'}
...: print 'hello %s %s, welcome to python world!' %(name,sex_dict.get(sex
...: ,u'先生'))
...:
In [39]: printh('evan',1)
hello evan 先生, welcome to python world!
return
如果这个function写在脚本里面执行呢 其实是什么都没有输出的,必定要使用return哦 哈哈
#!/usr/bin/python
#-*- coding:utf-8 -*-
def printhe(name,sex):
sex_dict = {1:u'先生',2:u'女士'}
#return print 'hello %s %s, welcome to python world!' %(name,sex_dict.get(sex,u'先生'))
mylove= 'hello'+' '+name + '' + sex_dict.get(sex,u'先生')+','+'welcome to python world!'
return mylove
print printhe('evan',1)
解决过程
一开始直接写到脚本里面执行,搞了半天,后来想到return 看到别人 str连接,于是灵机一动,终于去年print 而直接return str搞定喽 当然 也是我基础不够的原因吧 虽然两个小伙伴也是不会 果真要多动手,好过看一堆书,一开始是想解决位置参数的问题的 哈哈