习题4:变量和命名

发布时间:2017-11-07 14:07:20编辑:Run阅读(3966)

    从实践中学习,效果往往高于理论学习!

    代码如下:

    # coding: utf-8
    __author__ = 'www.py3study.com'
    cars = 100
    space_in_a_car = 4.0
    drivers = 30
    passengers = 90
    cars_not_driven = cars - drivers
    cars_driven = drivers
    carpool_capacity = cars_driven * space_in_a_car
    average_passengers_per_car = passengers / cars_driven
    
    print("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.")

    note

    "_"下划线这个符号在变量里通常被用作假象的空格,用来隔开单词,切记千万不要用"-"这个符号来连接单词

    应该看到的结果

    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.0 in each car.


    常见问题

    = 和 == 有什么不同

    ‘=’的作用是将右边的值赋予给左边的变量名,‘==’的作用是检查左右两边是否相等

    优雅的代码写法

    x = 100 而不是 x=100(虽然这样写并没有错误),强烈建议左右两边都加空格

关键字