标签:
# -*- coding: UTF-8 -*- you = "is" #对字符串you赋值为is,赋的值要用单引号或双引号括起来 cars = 100 # 定义cars这个变量并赋值为100 space_in_a_car = 4.0 # 定义space_in_a_car这个变量并赋值为4.0 drivers = 30 # 定义drivers这个变量并赋值为30 passengers = 90 # 定义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." #cars变量在输出的时候直接打印其内容 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." print "He %s there."%you #%s代表打印字符串变量,字符串%you这种格式要记牢
本课学到了给字符串和变量赋值
tips:如果是浮点型的数,直接在数字后加上.0
运行结果如下:
Hearn Python The Hard Way ex4 变量和命名
标签:
原文地址:http://www.cnblogs.com/Finn-Cheng/p/5084619.html