当我运行以下代码时:
def heart_value(name1,name2,name3):
if name1 == "Rachel":
return print("So you are Rachel!")
elif name2 == "Rachel":
print("Ah, so the second person is Rachel.")
return print('Right?')
elif name1 != 'Rachel':
return print('So Rachel is not the first person.')
else:
return print('ok')
print(heart_value("Rachel","Rachel","Mike"))它给了我以下内容:
,你就是瑞秋!无
为什么它还要打印None
发布于 2020-01-07 18:27:17
print函数不返回值。您只需要返回字符串return '<string>',而不是return print('<string>')。
它打印'None‘的原因是因为您对函数返回的值(即None)调用了None,因此该值出现在控制台中。
即
if name1 == "Rachel":
return "So you are Rachel!"
elif name2 == "Rachel":
return "Ah, so the second person is Rachel. \n Right?"
elif name1 != 'Rachel':
return 'So Rachel is not the first person.'
else:
return 'ok'
print(heart_value("Rachel","Rachel","Mike"))发布于 2020-01-07 18:36:26
print()函数执行一个操作(打印到控制台),但不返回任何内容。
因此,当您使用语句return(print("ok"))时,返回函数调用print( "ok“)函数,该函数在控制台中打印文本”ok“,但不向return()函数返回任何内容,这意味着值None被传递回最初称为heart_value()的函数。
您需要做的是return()字符串对象本身("ok")。这样,当您print()函数heart_value()时,print()语句将计算该函数,获取一个string对象,并打印该对象。
https://stackoverflow.com/questions/59634058
复制相似问题