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
    

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

  • 相关阅读:
    Create C++ Windows Forms Application in Visual Studio 2017
    VS项目打包发布
    获取光标所在行索引
    拖拽生成控件副本
    MDI中的ToolStrip合并
    使用FFmpeg音视频格式转换
    获取项目中其他文件
    解决Winform大多数DPI问题
    《Pro Git》第3章 分支
    《Pro Git》第2章 Git基础
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/9079819.html
Copyright © 2011-2022 走看看