Python04 naming of variables and variables

Source: Internet
Author: User

In my understanding, variables and algebra are basically a concept.

Ex4.py

cars = 100space_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_car = passengers / cars_drivenprint "There are", cars, "cars available."print "There are only", drivers, "drivers available."print "There will be", cars_not_driven, "empty cars today."print "We can transport", carpool_capacity, "people today."print "We have", passengers, "to carpool today."print "We need to put about", average_passengers_per_car, "in each car."

The words in rows 1-8 are variables. At the same time, we can see that the = number is used to assign values to the variables.

Slightly change the code

space_in_a_car = 4drivers = 30passengers = 90cars_not_driven = cars - driverscars = 100cars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_car = passengers / cars_drivenprint "There are", cars, "cars available."print "There are only", drivers, "drivers available."print "There will be", cars_not_driven, "empty cars today."print "We can transport", carpool_capacity, "people today."print "We have", passengers, "to carpool today."print "We need to put about", average_passengers_per_car, "in each car."

Run the following command:

To guess,Variables that are not assigned values cannot be used.

Modify the code.

cars space_in_a_car = 4drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_car = passengers / cars_drivenprint "There are", cars, "cars available."print "There are only", drivers, "drivers available."print "There will be", cars_not_driven, "empty cars today."print "We can transport", carpool_capacity, "people today."print "We have", passengers, "to carpool today."print "We need to put about", average_passengers_per_car, "in each car."

Sure enough.

I didn't understand why the word cars before nameerror showed "invalid argument ars". If you know it, let me know.

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.