The function contains a lot of things, which is difficult to understand, but don't worry. Check the list exercises in the previous section and finally understand.
You may not have noticed the details here. Now let's take a closer look: the variables in the function are not associated with the variables in the script.
See the following program:
[Python]
Def cheese_and_crackers (cheese_count, boxes_of_crackers ):
Print "You have % d cheeses! "% Cheese_count
Print "You hava % d boxes of crackers! "% Boxes_of_crackers
Print "Man that's enough for a party! "
Print "Get a blanket. \ n"
Print "We can just give the function numbers directly :"
Cheese_and_crackers (20, 30)
Print "OR, we can use variables from our script :"
Amount_of_cheese = 10
Amount_of_crackers = 50
Cheese_and_crackers (amount_of_cheese, amount_of_crackers)
Print "We can even do match inside too :"
Cheese_and_crackers (10 + 20, 5 + 6)
Print "And we can combine the two, variables and math :"
Cheese_and_crackers (amount_of_cheese + 100, amount_of_crackers + 1000)
The preceding shows all different methods for calling the cheese_and_crackers function.
The parameters in a function are similar to those in a variable. When assigning values to a variable, use =. The function also assigns values to the parameter.
Running result
Root @ he-desktop :~ /Mystuff # python ex19.py
We can just give the function numbers directly:
You have 20 cheeses!
You hava 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
OR, we can use variables from our script:
You have 10 cheeses!
You hava 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do match inside too:
You have 30 cheeses!
You hava 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two, variables and math:
You have 110 cheeses!
You hava 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.
Root @ he-desktop :~ /Mystuff #
Extra score exercise
1. Add comments to the script.
2. Read the code in turn.
3. Write a function by yourself and call it in 10 ways.
Author: lixiang0522