推荐学习书目
› 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 · Apr 1, 2015 · 5179 views
    This topic created in 4239 days ago, the information mentioned may be changed or developed.
    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入门经典》这本书上的例子。
    Supplement 1  ·  Apr 1, 2015
    32 replies  •  2015-04-01 21:05:18 +08:00
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    1
    缩进全没了~.~
    fkdtz
    fkdtz
    Apr 1, 2015
    2
    xsseroot
    xsseroot
    Apr 1, 2015
    3
    一看这么挫的格式,就不想继续看下去了。。。
    Pastsong
    Pastsong
    Apr 1, 2015
    4
    @commoccoom 用 gist 或者 markdown 的代码区块展示代码
    tb4649120073rs
    tb4649120073rs
    Apr 1, 2015
    5
    python没了缩进怎么看...
    cevincheung
    cevincheung
    Apr 1, 2015
    6
    python没有缩进看起来………………………… 好累
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    8
    @xsseroot
    @Pastsong
    @cevincheung
    @fkdtz
    @tb4649120073rs

    已经修改好了。多谢!
    mhycy
    mhycy
    Apr 1, 2015
    9
    js文本返回404状态码.....
    aaaa007cn
    aaaa007cn
    Apr 1, 2015
    10
    入门可以使用这个可视化工具单步执行来观察流程
    http://www.pythontutor.com/visualize.html

    另外
    不应是 print "Grand total:$()".format(grand_total)
    而应是 print "Grand total:${}".format(grand_total)
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    11
    @mhycy
    我手残,删除了。抱歉。。。。
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    12
    @mhycy

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

    有个grand_total变量用于计算每个列表的总额,并在每次循环最后都打出来....
    mhycy
    mhycy
    Apr 1, 2015
    17
    @aaaa007cn 简直神器!
    myself
    myself
    Apr 1, 2015
    18
    第一个缩进错了吧

    def print_seat(seat):
    ----for item in seat:
    --------print "${}".format(item)
    ----print "-"*15
    ----total = get_seat_total(seat)
    ----print "Total:${}".format(total)
    myself
    myself
    Apr 1, 2015
    19
    忽略上一条,已经是这样了
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    20
    @cheerzeng

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

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

    发现自己好蠢。。。
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    21
    @myself

    哦
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    22
    @aaaa007cn

    非常感谢!!!
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    23
    @vincentxue

    没有,只有一点点shell
    cheerzeng
    cheerzeng
    Apr 1, 2015
    24
    @commoccoom 哈哈,我也是第一次碰到,平时也是傻傻直接hardcode 15个*,所以要多看优秀的代码
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    25
    @cheerzeng

    嗯!
    vincentxue
    vincentxue
    Apr 1, 2015
    26
    @commoccoom 那就怪不得了,加油!
    commoccoom
    commoccoom
    OP
    Apr 1, 2015
    27
    @vincentxue

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

    你可能是不理解if __name__ == "__main__" =>这货就是其他语言中的main函数
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1240 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 16:02 · PVG 00:02 · LAX 08:02 · JFK 11:02
    ♥ Do have faith in what you're doing.