这个函数不会从脚本中调用变量吗?这与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)
答案 0 :(得分:5)
变量不会在任何时候连接到函数参数,除了它们作为参数传递给函数的事实。调用foo(bar)
不会将bar
连接到foo()
,它只会将bar
的值作为函数的第一个参数传递。如果该参数也恰好被称为“bar”,那么这就是巧合。