Learn Python The Hard Way学习(4) - 变量和命名
2012-06-14 22:32
661 查看
我们已经学习了打印和数学计算,下面来学习变量,在程序中,变量就是一个名称,让我们更加方便记忆。
如果你被下面的练习难住了,那么记住我们前面教的方法,找不同点,注意细节:
1. 为每行代码写注释。
2. 反过来读代码。
3. 读出你的代码。
提示:下划线一般用在变量名中表示假想的空格。让变量名的可读性高一点。
运行结果:
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#
加分练习
用自己的话解释一下下面的错误提示,使用行号并说明为什么?
第8行的变量car_pool_capacity没有定义,前面的定义的是carpool_capacity。
更多的练习:
1. 使用4.0作为space_in_a_car的变量的值有必要吗?如果用4的话会发生什么?
没有必要,人当然是整个的。
2. 记住4.0是浮点数,弄明白是什么意思。
3. 在变量赋值的行上加上注释。
4. 明白=号的作用是给变量赋值。
5. 记住_是下划线。
6. 把python作为计算器运行,使用变量进行运算。
如果你被下面的练习难住了,那么记住我们前面教的方法,找不同点,注意细节:
1. 为每行代码写注释。
2. 反过来读代码。
3. 读出你的代码。
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."
提示:下划线一般用在变量名中表示假想的空格。让变量名的可读性高一点。
运行结果:
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#
加分练习
用自己的话解释一下下面的错误提示,使用行号并说明为什么?
Traceback (most recent call last): File "ex4.py", line 8, in <module> average_passengers_per_car = car_pool_capacity / passenger NameError: name 'car_pool_capacity' is not defined
第8行的变量car_pool_capacity没有定义,前面的定义的是carpool_capacity。
更多的练习:
1. 使用4.0作为space_in_a_car的变量的值有必要吗?如果用4的话会发生什么?
没有必要,人当然是整个的。
2. 记住4.0是浮点数,弄明白是什么意思。
3. 在变量赋值的行上加上注释。
4. 明白=号的作用是给变量赋值。
5. 记住_是下划线。
6. 把python作为计算器运行,使用变量进行运算。
相关文章推荐
- Learn Python The Hard Way学习(18) - 命名,变量,代码,函数
- Learn Python The Hard Way学习(13) - 参数,解包,变量
- Learn Python The Hard Way学习(19) - 函数和变量
- Learn Python The Hard Way学习(5) - 更多的变量和打印
- 【Python】Learn Python the hard way, ex4 使用变量
- Learn Python The Hard Way学习(8) - 打印,还是打印
- Learn Python The Hard Way学习(40) - 模块,类和对象
- Learn Python The Hard Way学习(9) - 打印,打印,打印
- Learn Python The Hard Way学习(14) - 提示和传递
- Learn Python The Hard Way学习(20) - 函数和文件
- Learn Python The Hard Way学习(41) - 学习面向对象
- Learn Python The Hard Way学习(52) - 开始你的web游戏
- Learn Python The Hard Way学习(10) - 那是什么?
- Learn Python The Hard Way学习(15) - 读取文件
- Learn Python The Hard Way学习(3) - 数字和数学计算
- Learn Python The Hard Way学习(11) - 提问
- Learn Python The Hard Way学习(6) - 字符串和文本
- Learn Python The Hard Way学习(12) - 输入提示
- Learn Python The Hard Way学习(21) - 函数可以返回信息
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本