0

对于这个程序,我需要使用原始输入来告诉程序我想阅读哪个圣经经文。我必须能够使用,例如这些格式 gen 1 , gen 1:1 ,最后是 gen 1:1- 10 显示所选数据以供查看我尝试这样做,但它总是会说已超出列表。

 bibletext = open("kjv.txt" , "r").readlines()
 main = raw_input('')
 a = main.split()
 b = a[2].split(":")
 c = b[1].split("-")

 bible = {}
 for line in bibletext:
     number,bv,contents = line.split(" | ")
     book,ver = bv.strip().split(" ")
     chapter,verse = ver.strip().split(":")
     if book == a[1] and chapter == b[0] and verse == b[1]:
       print "%s , %s , %s" % (number, bv, contents)

这是我们必须使用的 txt 文件的示例。

0 | gen 1:1 | In the beginning God created the heaven and the earth. 
1 | gen 1:2 | And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters. 
2 | gen 1:3 | And God said, Let there be light: and there was light. 
3 | gen 1:4 | And God saw the light, that it was good: and God divided the light from the darkness. 
4 | gen 1:5 | And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. 
5 | gen 1:6 | And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. 
6 | gen 1:7 | And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so. 
7 | gen 1:8 | And God called the firmament Heaven. And the evening and the morning were the second day. 
8 | gen 1:9 | And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so. 
9 | gen 1:10 | And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good. 
10 | gen 1:11 | And God said, Let the earth bring forth grass, the herb yielding seed, and the fruit tree yielding fruit after his kind, whose seed is in itself, upon the earth: and it was so. 
4

2 回答 2

0

您可以使用以下代码,它将接受“ gen 1:1”或“ gen 1:3-5”形式的请求:

main = raw_input('')
a = main.split(" ")
b = a[1].split(":")
c = b[1].split("-")
verses = c[0] if len(c) == 1 else range(int(c[0]),int(c[1])+1)

bible = {}
for line in bibletext.splitlines():
    number,bv,contents = line.split(" | ")
    book,ver = bv.strip().split(" ")
    chapter,verse = ver.strip().split(":")
    for i in verses:
        if book == a[0].strip() and chapter == b[0].strip() and verse == str(i):
            print "%s, %s" % (bv, contents)
于 2013-07-26T08:41:06.853 回答
0

它是因为如果使用 gen 1:1 作为输入并且你打印a你得到['gen', '1:1']

这意味着没有a[2]公正a[0]a[1]

所以如果你尝试使用a[2]你会得到一个错误,说列表索引超出范围

所以改为:

b = a[1].split(":")
于 2013-07-26T08:36:45.073 回答