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
Tianny
V2EX  ›  Python

一道 python 题,大家给帮帮忙, thx!

  •  
  •   Tianny · 2016-10-28 00:58:59 +08:00 · 1706 次点击
    这是一个创建于 2739 天前的主题,其中的信息可能已经有所发展或是发生改变。

    题目如下: 写一个逐页显示文本文件的程序. 提示输入一个文件名, 每次显示文本 文件的 25 行, 暂停并向用户提示"按任意键继续.", 按键后继续执行

    6 条回复    2016-10-29 09:12:31 +08:00
    eyp82
        1
    eyp82  
       2016-10-28 07:20:51 +08:00   ❤️ 1
    随手撸了一个, 代码有点烂, 哪里有 bug 还请各位不吝指出:


    #!/usr/bin/env python3


    def show_content(file, paginate):
    """Show the content of a file, display 'paginate'
    lines and ask user to press any key to continue"""
    with open(file) as f:
    for idx, line in enumerate(f, start=1):
    print(line, end='')
    if not idx % paginate:
    print('Press any key to continue')
    _ = input()


    if __name__ == '__main__':
    show_content('test.out', 25)
    eyp82
        2
    eyp82  
       2016-10-28 07:22:44 +08:00
    缩进都被吃掉了....
    Tianny
        3
    Tianny  
    OP
       2016-10-28 11:12:24 +08:00
    @eyp82 thx !我再把缩进的给补回来
    #!/usr/bin/env python3

    def show_content(file, paginate):
    """show the content of a file,display paginate lines
    and ask user to press any key to continue"""

    with open(file) as f:
    for idx, line in enumerate(f, start=1):
    print (line,end='')
    if not idx % paginate:
    print ('Press any key to continue')
    _ = input()

    if __name__ == '__main__':
    show_content('/home/tiantian/.vimrc', 25)
    Tianny
        4
    Tianny  
    OP
       2016-10-28 11:13:54 +08:00
    @eyp82 ...我的也被吃掉了。。
    btjoker
        5
    btjoker  
       2016-10-28 20:55:36 +08:00
    @Tianny 试试全角空格
    yexiaoxing
        6
    yexiaoxing  
       2016-10-29 09:12:31 +08:00 via Android
    试试 gist …
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4579 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:09 · PVG 09:09 · LAX 18:09 · JFK 21:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.