概念上一个是 返回值 一个是打印输出
区别一:return是结束语一般放在函数的最后,当你在return 结束后面再写一些东西是不执行的如 下
def renshu(x,y): h=x+y print (h) return h print (‘hello word‘) print (renshu(3,7)) 执行后的结果是 Hello word 不执行。没有报错 C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/python/class/suibi.py1010
1 C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/python/class/suibi.py 2 10 3 10
区别二return 返回的结果可以调用在别去如做相加
def renshu(x,y): h=x+y print (h) return h print (‘hello word‘) print (renshu(3,7)) x= (renshu(3,7))+10print (x)
执行结果是C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/python/class/suibi.py10101020 Process finished with exit code 0
print 仅仅只是打印没有结果
def renshu(x,y): h=x+y print (h) print (renshu(3,7)) x= (renshu(3,7))+10print (x) 报错信息
None
File "C:/Users/Administrator/PycharmProjects/python/class/suibi.py", line 13, in <module>
10
x= (renshu(3,7))+10
TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘int‘
Process finished with exit code 1
时间: 2024-10-11 12:19:41