【发布时间】:2015-05-11 10:03:00
【问题描述】:
this exercise的额外学分问题:
问:为什么你把变量叫做
jelly_beans,而把名字叫做beans稍后?答:这是函数工作原理的一部分。请记住,在 函数变量是临时的。当你返回它时,它可以是 分配给一个变量供以后使用。我只是在制作一个名为的新变量
beans保存返回值。
“函数内部的变量是临时的”是什么意思?这是否意味着变量在return 之后无效?好像函数缩进后,我无法打印函数部分使用的变量。
从答案中它说“当你返回它时,它可以分配给一个变量供以后使用”。有人能解释一下这句话吗?
print "Let's practice everything."
print 'You\'d need to know \'bout escape with \\ that do \n newlines and \t tabs.'
poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print "-------------"
print poem
print "-------------"
five = 10 - 2 + 3 - 6
print "This should be five: %s" % five
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
print "With a starting point of : %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)
start_point = start_point / 10
print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
【问题讨论】:
标签: python function python-2.7