Learn Python The Hard Way learning (4)-variables and names

Source: Internet
Author: User

We have learned printing and mathematical computation. Next we will learn variables. in the program, variables are a name, which makes it easier for us to remember.

If you are overwhelmed by the following exercises, remember the methods we taught before and find different points. Pay attention to the details:
1. Write comments for each line of code.
2. Read the code in turn.
3. Read your code.

[Python]
1. cars = 100
2. space_in_a_car = 4.0
3. drivers = 30
4. passengers = 90
5. cars_not_driven = cars-drivers
6. cars_driven = drivers
7. carpool_capacity = cars_driven * space_in_a_car
8. average_passengers_per_car = passengers/cars_driven
9.
10.
11. print "There are", cars, "cars available ."
12. print "There are only", drivers, "drivers available ."
13. print "There will be", cars_not_driven, "empty cars today ."
14. print "We can transport", carpool_capacity, "people today ."
15. print "We have", passengers, "to carpool today ."
16. print "We need to put about", average_passengers_per_car, "in each car ."

Tip: underlines are generally used in variable names to indicate hypothetical spaces. Make variable names more readable.

Running result:
Root @ he-desktop :~ /Mystuff # python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
Root @ he-desktop :~ /Mystuff #

Extra score exercise
In your own words, explain the following error message, use the row number, and explain why?
Traceback (most recent call last ):
File "ex4.py", line 8, in <module>
Average_passengers_per_car = car_pool_capacity/pasity
NameError: name 'car _ pool_capacity 'is not defined
The variable car_pool_capacity of Row 3 is not defined. The previous variable carpool_capacity is defined.

More exercises:
1. Is it necessary to use 4.0 as the value of the space_in_a_car variable? What will happen if 4 is used?
There is no need, of course, people are the whole. Www.2cto.com

2. Remember that 4.0 is a floating point number to understand what it means.

3. Add comments to the variable assignment line.

4. Understand that the value = is used to assign values to variables.

5. Remember that _ is an underline.

6. Run python as a calculator and use variables for calculation.

Author: lixiang0522

Related Article

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.