V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
commoccoom
V2EX  ›  Python

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

  •  
  •   commoccoom · 2015-04-01 14:34:51 +08:00 · 4096 次点击
    这是一个创建于 3307 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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入门经典》这本书上的例子。
    第 1 条附言  ·  2015-04-01 15:24:14 +08:00
    32 条回复    2015-04-01 21:05:18 +08:00
    commoccoom
        1
    commoccoom  
    OP
       2015-04-01 14:35:37 +08:00
    缩进全没了~.~
    fkdtz
        2
    fkdtz  
       2015-04-01 14:37:09 +08:00
    xsseroot
        3
    xsseroot  
       2015-04-01 14:39:41 +08:00
    一看这么挫的格式,就不想继续看下去了。。。
    Pastsong
        4
    Pastsong  
       2015-04-01 14:40:23 +08:00
    @commoccoom 用 gist 或者 markdown 的代码区块展示代码
    tb4649120073rs
        5
    tb4649120073rs  
       2015-04-01 14:40:37 +08:00
    python没了缩进怎么看...
    cevincheung
        6
    cevincheung  
       2015-04-01 14:41:26 +08:00
    python没有缩进看起来………………………… 好累
    commoccoom
        7
    commoccoom  
    OP
       2015-04-01 15:10:45 +08:00
    commoccoom
        8
    commoccoom  
    OP
       2015-04-01 15:12:24 +08:00
    @xsseroot
    @Pastsong
    @cevincheung
    @fkdtz
    @tb4649120073rs

    已经修改好了。多谢!
    mhycy
        9
    mhycy  
       2015-04-01 15:17:04 +08:00
    js文本返回404状态码.....
    aaaa007cn
        10
    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
        11
    commoccoom  
    OP
       2015-04-01 15:22:05 +08:00
    @mhycy
    我手残,删除了。抱歉。。。。
    commoccoom
        12
    commoccoom  
    OP
       2015-04-01 15:24:42 +08:00
    @mhycy

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

    有个grand_total变量用于计算每个列表的总额,并在每次循环最后都打出来....
    mhycy
        17
    mhycy  
       2015-04-01 15:37:48 +08:00
    @aaaa007cn 简直神器!
    myself
        18
    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
        19
    myself  
       2015-04-01 16:11:02 +08:00
    忽略上一条,已经是这样了
    commoccoom
        20
    commoccoom  
    OP
       2015-04-01 16:14:42 +08:00
    @cheerzeng

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

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

    发现自己好蠢。。。
    commoccoom
        21
    commoccoom  
    OP
       2015-04-01 16:15:48 +08:00
    @myself

    commoccoom
        22
    commoccoom  
    OP
       2015-04-01 16:16:11 +08:00
    @aaaa007cn

    非常感谢!!!
    commoccoom
        23
    commoccoom  
    OP
       2015-04-01 16:18:16 +08:00
    @vincentxue

    没有,只有一点点shell
    cheerzeng
        24
    cheerzeng  
       2015-04-01 16:20:30 +08:00
    @commoccoom 哈哈,我也是第一次碰到,平时也是傻傻直接hardcode 15个*,所以要多看优秀的代码
    commoccoom
        25
    commoccoom  
    OP
       2015-04-01 16:27:32 +08:00
    @cheerzeng

    嗯!
    vincentxue
        26
    vincentxue  
       2015-04-01 16:28:21 +08:00
    @commoccoom 那就怪不得了,加油!
    commoccoom
        27
    commoccoom  
    OP
       2015-04-01 16:38:28 +08:00
    @vincentxue

    谢谢!
    geeux
        28
    geeux  
       2015-04-01 17:22:39 +08:00
    +油
    wind3110991
        29
    wind3110991  
       2015-04-01 19:43:03 +08:00
    markdown一下,tab首行缩紧没了,python语言的特点不能丢啊
    julyclyde
        30
    julyclyde  
       2015-04-01 20:15:12 +08:00
    从if开始的;之前的def只定义了函数,但并没有其它能让人感觉出来的实质内容
    lyoe
        31
    lyoe  
       2015-04-01 20:50:46 +08:00
    执行一遍看下结果,很容易就理解的
    ming2281
        32
    ming2281  
       2015-04-01 21:05:18 +08:00
    哈哈,v2ex的Markdown渲染还是不懂啊
    为毛我提的问题没有渲染成Markdown
    粗看了一下=>好像就是答应seat的一些信息吧

    你可能是不理解if __name__ == "__main__" =>这货就是其他语言中的main函数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2750 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:14 · PVG 23:14 · LAX 08:14 · JFK 11:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.