zoukankan      html  css  js  c++  java
  • Python基础教程(第2版•修订版)代码清单2-3 勘误

    这几天在看《Python基础教程(第2版•修订版)》,期间2-3的代码越看越感觉有问题……奈何宿舍晚上准时断网,只能今早起来写……

    代码清单2-3 序列(字符串)乘法示例 是一个很简单的例子,和ACM里的打印字符串的题类似。仔细看看书上的代码,就会发现该例子的运行情况和书本上的并不一致,如下图:
    课本示例运行情况
    书本上的代码:

    sentence = raw_input("Sentence: ")
    
    screen_width = 80
    text_width = len(sentence)
    box_width = text_width + 6
    left_margin = (screen_width - box_width) // 2
    
    print
    print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
    print ' ' * left_margin + '|' + ' ' * text_width    + '|'
    print ' ' * left_margin + '|' +       sentence      + '|'
    print ' ' * left_margin + '|' + ' ' * text_width    + '|'
    print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
    print
    

    遂添加了几处,改动后的代码如下:

    sentence = raw_input("Sentence: ")
    
    screen_width = 80
    text_width = len(sentence)
    box_width = text_width + 18
    left_margin = (screen_width - box_width) // 2
    
    print
    print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
    print ' ' * left_margin +  ' ' * 7 + '|' + ' ' + ' ' * text_width + ' ' + '|'
    print ' ' * left_margin +  ' ' * 7 + '|' + ' ' +       sentence   + ' ' + '|'
    print ' ' * left_margin +  ' ' * 7 + '|' + ' ' + ' ' * text_width + ' ' + '|'
    print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
    print
    

    运行情况如下图:
    实际达到的运行情况

  • 相关阅读:
    go语言】Goroutines 并发模式
    Mysql Innodb 引擎优化 参数(innodb_buffer_pool_size)
    多key业务,数据库水平切分架构一次搞定
    Goroutine是如何工作的?
    PHP进程之信号捕捉中的declare(ticks=1)
    php多进程总结
    mysql强制性操作
    rabbitMQ高可用
    服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
    mysql在innodb索引下b+树的高度问题。
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/9079819.html
Copyright © 2011-2022 走看看