前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >习题4:变量和命名

习题4:变量和命名

作者头像
py3study
发布2018-08-02 10:50:52
2400
发布2018-08-02 10:50:52
举报
文章被收录于专栏:python3python3

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

代码如下:

代码语言:javascript
复制
# 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(虽然这样写并没有错误),强烈建议左右两边都加空格

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档