更新时间:2022-04-25 10:08:32
assign_coin_to_bag
的范围,并且无法在 main
中进行访问。您在 coins_in_the_bag
中捕获了 assign_coin_to_bag
的返回值,并且需要在调用 show_total
。
coin_to_bag
is a defined in the scope of assign_coin_to_bag
, and is not accessible in main
. You caught the return value of assign_coin_to_bag
in coins_in_the_bag
, and need to use that variable in your call to show_total
.
新程序员做出一个常见错误,认为无论使用哪个变量,变量的名称都应该相同甚至跨越方法。事实上,变量的名称对计算机来说绝对没有任何意义。
There's a common error new programmers make, which is thinking the name of a variable needs to be the same wherever it's used, even across methods. In fact, the name of the variable means absolutely nothing to the computer. Good names are only there for humans.
作为练习,我们过去让学生跟踪这样的代码:
As an exercise, we used to have students trace code like this:
def foo(one, three, two):
print "%s %s %s" % (one, two, three)
def bar():
return 1, 2, 4
three, two, one = bar()
foo(two, one, three)
找出打印的内容,这将是一个很好的练习来打破变量命名习惯。
Figure out what gets printed, and that will be good practice to break the variable naming habit.