Closed lzblack closed 2 years ago
老师好!我把quiz改完啦!return这个东西我还是有点困惑,您可以再帮我看下我用的对吗?如果我一直写print,在方程的最后return a,b, 是不是也可以啊,就是不符合题目要求?
如果我一直写print,在方程的最后return a,b, 是不是也可以啊,就是不符合题目要求?
不太明白你的意思。
不过总的原则是:你作为这个function 的创造者,你要自己心里清楚这个function 是用来输出 (print) 的,还是用来返回 (return) 值的。
用 return True
就行。第一不用加括号,第二其实我要求的是返回 bool 值。
https://github.com/michael4227/OIM3640/blob/cdb0d8e71e3100858e60d9c6c3a5c1fe573d1fa4/Quiz/quiz1.py#L9-L22
尽量不要用 sum 当作变量名,因为 it shadows built-in name `sum: https://github.com/michael4227/OIM3640/blob/cdb0d8e71e3100858e60d9c6c3a5c1fe573d1fa4/Quiz/quiz1.py#L49-L57
Overall, good job!
那就是说return的结果既可以是一个variable,也可以是一个string,然后如果return的是一个string的话,以后call function的时候跑出来的结果和 function里写print一个string跑出来的结果是一样的
比如说,
def func(n):
print(n)
func('apple')
和
def func(n):
return (n)
print(func('apple'))
这两种写法可以达到同样的目的,就是在call function的时候不一样?
从你给的单个例子来说,效果是一样的。但是,如果又有其它代码要用到 func
的返回值,那就完全不一样了。比如:
sentence = 'I like ' + func("apple")
print(sentence)
明白了,谢谢老师!
Zhi Li @.***>于2021年10月4日 周一下午4:30写道:
从你给的单个例子来说,效果是一样的。但是,如果又有其它代码要用到 func 的返回值,那就完全不一样了。比如:
sentence = 'I like ' + func("apple")
print(sentence)
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/michael4227/OIM3640/issues/7#issuecomment-933831929, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVNWBDZKRND3EBGPOYPKT5DUFIFGDANCNFSM5FFFCK7Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
--
return
notprint
: https://github.com/michael4227/OIM3640/blob/37e5cfde4f17c9d1d014c9d69653b9d56822b79f/Quiz/quiz1.py#L9-L22==
or=
: https://github.com/michael4227/OIM3640/blob/37e5cfde4f17c9d1d014c9d69653b9d56822b79f/Quiz/quiz1.py#L57 Please keep working on this quiz.