python变量四大命名_笨方法学Python 习题4:变量和命名

数字和数学计算

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.")

刚开始写这个程序时我把space_in_a_car=4.0中的space拼成了sapce,导致运行的时候powershell提示:

> python .\ex4.py

Traceback (most recent call last):

File ".\ex4.py", line 7, in

carpool_capacity=cars_driven*space_in_a_car

NameError: name 'space_in_a_car' is not defined

无法定义第七行拼写正确的space_in_a_car变量。其实不止是拼写错误,变量多一个下划线也无法定义。

改成相同的变量后,正确的运行结果:

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

加分习题

Q1.我在程序里用了 4.0 作为 space_in_a_car 的值,这样做有必要吗?如果只用 4会有什么问题?

我们将浮点数4.0改为整数4:

cars=100

space_in_a_car=4

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.")

运行结果:

> 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 people today.

We have 90 to carpool today.

We need to put about 3.0 in each car.

可以看出,在不需要进行浮点运算的程序中,将浮点数改为整数不会有任何影响。

Q2.记住 4.0 是一个“浮点数”,自己研究一下这是什么意思。

Q3.在每一个变量赋值的上一行加上一行注解。

#将100赋值给变量cars

cars=100

#将4.0赋值给变量space_in_a_car

space_in_a_car=4.0

#将30赋值给变量drivers

drivers=30

#将90赋值给变量passengers

passengers=90

#将cars-drivers赋值给变量cars_not_driven

cars_not_driven=cars-drivers

#将drivers赋值给变量cars_driven

cars_driven=drivers

#将cars_driven*space_in_a_car赋值给变量carpool_capacity

carpool_capacity=cars_driven*space_in_a_car

#将passengers/cars_driven赋值给变量average_passengers

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.")

Q4.记住 = 的名字是等于(equal),它的作用是为东西取名。

Q5.记住 _ 是下划线字符(underscore)。

Q6.将 python 作为计算器运行起来,就跟以前一样,不过这一次在计算过程中使用变量名来做计算,常见的变量名有 i, x, j 等等。

i = "ABC"

x = i

i = "XYZ"

print(x)

这里我先将"ABC"赋值给变量"i",将"i"赋值给变量"x",再将"XYZ"赋值给变量"i",来看看运行结果:

> python .\ex4_drills.py

ABC

这里的运行结果为"ABC",而不是"XYZ",这里程序运行分三个步骤:执行i="ABC",创建了字符串"ABC"和变量"i",并把"i"指向"ABC"。

执行x=i,创建了变量"x",并把x指向i指向的字符串"ABC"。

执行i="XYZ",创建了字符串"XYZ",并把i的指向改为"XYZ",但x并没有更改。

所以,最后打印变量x的结果自然是"ABC"了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值