0

page1.txt、page2.txt、page3.txt、page4.txt という名前のファイルがあります。これらはすべてのページです。ユーザー入力が求められ、ユーザーが 1 を押すと、ページ番号 1 が表示されます。ユーザーがページ番号を押すと、次のように結果を表示できます。

def read(argu):
    argu = open(argu)
    y = [x for x in argu]
    print y


inp = raw_input('Say: ')

if inp=='1':
    read('page1.txt')

if inp=='2':
    read('page2.txt')

if inp=='3':
    read('page3.txt')

次と前に行き詰まっています。ユーザーがページ2にいて、次を押すと、ページ3などを表示する必要があります。それ、どうやったら出来るの?前もって感謝します。

4

1 に答える 1

3

In order to move from one page to another, you need to use a variable to show what page the user is currently reading. Previous and Next would then update the page number and display the appropriate page:

file = ['page1.txt', 'page2.txt', 'page3.txt', 'page4.txt']
pagecount = len(file)
page = 1                    # initialize to a default page

if inp == '1':
    page = 1
    read(file[page-1])      # pages are 1-4, subscripts are 0-3

# ... pages 2-4 go here 

elif inp == '+':              # whatever key you use for Next
    page = min(page+1, pagecount)    # don't go beyond last page
    read(file[page-1])

elif inp == '-':              # switch statements use "if .. elif .. elif .. else"
    page = max(page-1, 1)
    read(file[page-1])

After you get that version working, you can generalize it to allow an arbitrary number of pages by constructing the file name from the page number instead of storing filenames in a list. And you only need one "read" for your input loop -- since every key reads a page you can factor that out of each individual key.

于 2012-09-18T11:56:26.927 に答える