Learn Python The Hard Way learning (19)-functions and variables

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.