通过这个练习,你看到我们给我们的函数 cheese_and_crackers 很多的参数,
然后在函数里把它们打印出来。我们可以在函数里用变量名,我们可以在函数里
做运算,我们甚至可以将变量和运算结合起来。
def cheese_and_crackers(cheese_count,boxes_of_crackers):
print("You have %d cheese." %cheese_count)
print("And you also have %d boxes of crackers." %boxes_of_crackers)
print("Man that's enough for a party!")
print("Get a blanket.\n")
print("可以直接给两个参数进行赋值:")
cheese_and_crackers(10,20)
print("We can use variables for the scripts:")
amount_of_chess = 30
amount_of_crackers = 40
cheese_and_crackers(amount_of_chess,amount_of_crackers)
print("We can even do math inside too:")
cheese_and_crackers(1+2,3+4)
print("And we can combine the two,variables and math")
cheese_and_crackers(amount_of_chess+3,amount_of_crackers+5)
运行结果
可以直接给两个参数进行赋值:
You have 10 cheese.
And you also have 20 boxes of crackers.
Man that’s enough for a party!
Get a blanket.
We can use variables for the scripts:
You have 30 cheese.
And you also have 40 boxes of crackers.
Man that’s enough for a party!
Get a blanket.
We can use variables for the scripts:
You have 30 cheese.
And you also have 40 boxes of crackers.
Man that’s enough for a party!
Get a blanket.
We can even do math inside too:
You have 3 cheese.
And you also have 7 boxes of crackers.
Man that’s enough for a party!
Get a blanket.
And we can combine the two,variables and math
You have 33 cheese.
And you also have 45 boxes of crackers.
Man that’s enough for a party!
Get a blanket.
总结
在使用函数的时候,可以对里面的变量进行数学运算,也可以定义新的变量,用新变量来表示函数的参数