入门 Python ,有一段脚本看不明白,望指教。谢谢!

2015-04-01 14:34:51 +08:00
 commoccoom
def print_seat(seat):
for item in seat:
print "${}".format(item)
print "-"*15
total = get_seat_total(seat)
print "Total:${}".format(total)

def get_seat_total(seat):
total = 0
for dish in seat:
total = total+dish
return total

def main():
seats = [[19.95],[20.45+3.10],[7.00/2,2.10,21.45],[7.00/2,2.10,14.99]]

grand_total = 0
for seat in seats:
print_seat(seat)
grand_total = grand_total+get_seat_total(seat)
print "\n"
print "="*15
print "Grand total:$()".format(grand_total)

if __name__=="__main__":
main()


——————————————————————————————————————————————
搞不懂这段程序的流程,《Python入门经典》这本书上的例子。
4102 次点击
所在节点    Python
32 条回复
commoccoom
2015-04-01 14:35:37 +08:00
缩进全没了~.~
fkdtz
2015-04-01 14:37:09 +08:00
xsseroot
2015-04-01 14:39:41 +08:00
一看这么挫的格式,就不想继续看下去了。。。
Pastsong
2015-04-01 14:40:23 +08:00
@commoccoom 用 gist 或者 markdown 的代码区块展示代码
tb4649120073rs
2015-04-01 14:40:37 +08:00
python没了缩进怎么看...
cevincheung
2015-04-01 14:41:26 +08:00
python没有缩进看起来………………………… 好累
commoccoom
2015-04-01 15:10:45 +08:00
commoccoom
2015-04-01 15:12:24 +08:00
@xsseroot
@Pastsong
@cevincheung
@fkdtz
@tb4649120073rs

已经修改好了。多谢!
mhycy
2015-04-01 15:17:04 +08:00
js文本返回404状态码.....
aaaa007cn
2015-04-01 15:20:33 +08:00
入门可以使用这个可视化工具单步执行来观察流程
http://www.pythontutor.com/visualize.html

另外
不应是 print "Grand total:$()".format(grand_total)
而应是 print "Grand total:${}".format(grand_total)
commoccoom
2015-04-01 15:22:05 +08:00
@mhycy
我手残,删除了。抱歉。。。。
commoccoom
2015-04-01 15:24:42 +08:00
@mhycy

已经修复。谢谢
vincentxue
2015-04-01 15:33:02 +08:00
楼主有没有其他语言经验?
cheerzeng
2015-04-01 15:34:12 +08:00
@commoccoom 现在流程搞懂了吗?
vincentxue
2015-04-01 15:35:08 +08:00
@aaaa007cn 提供的工具相当不错啊。。
mhycy
2015-04-01 15:36:18 +08:00
看起来是获取总价的样子...
第一个函数前半部分用于打印出一个价目列表, total = 那里是获取一个总额,就是这个列表的总额
第二个函数用于第一个函数获取总额部分,遍历列表,数字相加,返回总额数字

有个grand_total变量用于计算每个列表的总额,并在每次循环最后都打出来....
mhycy
2015-04-01 15:37:48 +08:00
@aaaa007cn 简直神器!
myself
2015-04-01 16:10:24 +08:00
第一个缩进错了吧

def print_seat(seat):
----for item in seat:
--------print "${}".format(item)
----print "-"*15
----total = get_seat_total(seat)
----print "Total:${}".format(total)
myself
2015-04-01 16:11:02 +08:00
忽略上一条,已经是这样了
commoccoom
2015-04-01 16:14:42 +08:00
@cheerzeng

试了10楼的方法,总算是看明白了。

另外,一直在纠结 print "-"*15是什么意思,原来是划线的意思。。。。。

发现自己好蠢。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/180853

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX