【发布时间】:2016-05-20 18:52:13
【问题描述】:
我在 Debian Linux 上使用 Python 2.7.11。
我有两个函数,一个与普通函数调用完全一样,另一个运行良好,除了普通函数调用不适用于第二个函数;我必须在函数前面放一个 print 才能让它工作。
1) 第一个函数,通过正常的函数调用按预期执行:
def print_me(string):
print string
print_me("I am a string")
2)第二个函数,它不适用于普通函数调用:
def fruit_color(fruit):
fruit = fruit.lower()
if fruit == 'apple':
return 'red'
elif fruit == 'banana':
return 'yellow'
elif fruit == 'pear':
return 'green'
else:
return 'Fruit not recognized'
3) 正常的函数调用,即fruit_color('apple'),不起作用。相反,我必须将 print 放在函数前面才能使其工作:
print fruit_color('apple')
4) 既然我已经(希望)简洁地解释了自己,我将重申我的问题:为什么函数调用适用于 print_me 函数而不适用于 fruit_color 函数?
【问题讨论】:
-
print打印,return不打印,所以需要显式打印返回值。