本文介绍了以艰难的方式学习Python - 练习24的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的额外信用问题:

The extra credit question for this exercise:

A:这是功能如何工作的一部分。请记住,在
函数中,变量是临时的。当你返回它时,它可以被
分配给一个变量供以后使用。我只是创建了一个名为
beans 的新变量来保存返回值。

A: That's part of how a function works. Remember that inside the function the variable is temporary. When you return it then it can be assigned to a variable for later. I'm just making a new variable named beans to hold the return value.

函数中的变量是暂时的是什么意思?这是否意味着该变量在 return 之后无效?这似乎是在函数缩进之后,我无法打印函数部分中使用的变量。

What does "the variable inside the function is temporary" mean? Does that mean the variable is not valid after the return? It seems like after the indentation of the function, I can't print the variable used in the function part.

从答案中可以看出当你返回那么它可以被分配给一个变量,以供以后使用。有人可以解释一下这句话吗?

From the answer it says "When you return it then it can be assigned to a variable for later". Could someone explain that sentence a bit please?

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)


推荐答案

是的;当函数结束所有本地范围的名称( jelly_beans ,在您的示例中)不再存在。名称 jelly_beans 只能在 secret_formula 中访问。

Yes; when the function ends all locally-scoped names (jelly_beans, in your example) cease to exist. The name jelly_beans is only accessible within secret_formula.

即使通过函数名称,也不能从函数外部访问它们(所以既不是 jelly_beans 也不是 secret_formula.jelly_beans 给你访问值)。这实际上是一件好事,因为它意味着你可以在函数中封装内部逻辑,而不会将其暴露给程序的其余部分。

You cannot access them from outside the function, even via the function name (so neither jelly_beans nor secret_formula.jelly_beans give you access to the value). This is actually a good thing, as it means that you can encapsulate the internal logic within the function without exposing it to the rest of your program.

这答案,它说的 当您返回它,然后它可以被分配给一个变量为后来的的。

只删除函数中的本地名称,不一定是它们引用的对象。当返回jelly_beans,jars,crates 时,它会将对象(而不是名称)传递回任何名为 secret_formula 。你可以给这些对象使用与函数外部相同的名字或者完全不同的东西:

Only the local names inside the function are deleted, not necessarily the objects they reference. When you return jelly_beans, jars, crates, this passes the objects (not the names) back to whatever called secret_formula. You can give the objects the same names outside the function or something completely different:

foo, bar, baz = secret_formula(...)

介绍了如何在Python中命名。

This article is a useful introduction to how naming works in Python.

这篇关于以艰难的方式学习Python - 练习24的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 16:00