Python函数和变量 - 学习Python ex19的难点

Ric*_*cky -1 python variables function python-2.7

这个函数不是从脚本中调用变量吗?这与Zed Shaw所说的相反......

我把评论放在自己身上,我的问题是这样的:

Zed Shaw说:

函数中的变量未连接到脚本中的变量

但是从我看到的,函数在缩进的顶部,然后当缩进开始创建变量然后链接到函数.

这个函数不是从脚本中调用变量吗?

我找不到合适的答案:有人可以详细说明这段脚本并告诉我,我是否错误地查看了它?

# This is the function with declared variables inside
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print  "You have %d cheeses!" % cheese_count
    print "You have %d boxes of crackers!"  % boxes_of_crackers
    print "Man thats enough for a party!"
    print "Get a Blanket. \n"

# This declares the amounts in the functions variables
print "We can just give the function numbers directly:"
cheese_and_crackers (20, 30)

# This variable sets the amounts
print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
#This variable combines the two above and stores it in a single variable for the function to call
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

# This uses the function, but has predefined variables with maths
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)
#This combines variables with maths
print "And we can combine the two, variables and math:"
cheese_and_crackers (amount_of_cheese + 100, amount_of_crackers + 1000)
Run Code Online (Sandbox Code Playgroud)

Ign*_*ams 5

变量不会在任何时候连接到函数参数,除了它们作为参数传递给函数的事实.呼叫foo(bar)不连接barfoo(),它仅仅传递的值bar作为该函数的第一参数.如果该参数恰好也被称为"bar",那么这就是巧合.