文章目录
系统:mac os 10.14
Python: 2.7.10
版本:《笨办法学Python》(第四版)
基本习题
1. 完成基本习题
(1) 根据题目,编辑以下内容:
附代码:
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 are 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."
(2)执行代码,结果如下:


这篇博客详细解析了《笨办法学Python》中的基本习题和加分习题,包括变量的定义与使用,浮点数的概念,以及如何处理计算中的精度问题。作者通过实例解释了错误信息"name 'car_pool_capacity' is not defined"的原因,并讨论了在程序中使用4.0与4作为变量值的区别。此外,还强调了变量注释的重要性以及如何用Python进行变量计算。
最低0.47元/天 解锁文章
2503





