繁体   English   中英

Python 错误:IndexError:字符串索引超出家谱程序范围

[英]Python Error: IndexError: string index out of range for a Family Tree Program

所以我正在制作这个家谱程序,我有两个文件:

姓氏.txt

Nerk,Sam,M,1
Nerk,Aileen,F,2
Nerk,Peter,M,3
Smith,Cathy,F,5
Nerk,Matthew,M,7
Nerk,Janine,F,21
Martin,Marion,F,22
Nerk,Louise,F,8
Nerk,Melissa,F,9
Nerk,Kim,F,10
Nerk,Luke,M,11
Smith,Greg,M,12
Smith,Marta,F,17
Smith,Isaac,M,18
Nerk,Eliza,F,19
Nerk,Henry,M,20
Nerk,Karina,F,28

和 ParentChild.txt

1,8
1,9
1,10
1,11
2,1
2,5
2,7
3,1
3,5
3,7
12,17
12,18
5,17
5,18
7,19
7,20
28,19
28,20
22,8
22,9
22,10
27,21
21,11

FamNames 存储 Lastname, Firstname, Gender, ID ParentChild 存储父母的 ID,孩子的 ID

这是我用于查找用户想要的任何人的孩子的代码:

#Create empty arrays
LastName = []
FirstName = []
Gender = []
ID = []
ParentID = []
ChildID = []

#Assign the correct data to the arrays
import csv
f = open('FamNames.txt')
for row in csv.reader(f):
        LastName.append(row[0])
        FirstName.append(row[1])
        Gender.append(row[2])
        ID.append(row[3])
f.close()
f = open('ParentChild.txt')
for row in csv.reader(f):
        ParentID.append(row[0])
        ChildID.append(row[1])
f.close()

#Prints out the first and last names of everyone with a line number infront
n = 0
while n < 17: #number of lines in FamNames.txt
        print('Line', n, FirstName[n], LastName[n])
        n = n + 1

#User selects who they want to view family members of
i = int(input("\nPlease type in line number for whom you'd like to see the family members:\n"))
print("\nYou've selected to see the family members of:", FirstName[i], LastName[i])

#Finds the line on which the ID is located in ParentChild.txt
ID = ID[i]
t = 0
while t < 23: #number of lines in ParentChild.txt
        if ID == ParentID[t]: #n will store the line on which the parent is
                break
        t = t + 1

#Finds the line on which the child is located in FamNames.txt
child = ChildID[t]
x = 0
while x < 17: #number of lines in FamNames.txt
        if child == ID[x]: #n will store the line on which the child is
                break
        x = x + 1

#Prints out the childs name
print('The child of', FirstName[i], LastName[i], 'is:', FirstName[x], LastName[x])

这是输出/错误:

Line 0 Sam Nerk
Line 1 Aileen Nerk
Line 2 Peter Nerk
Line 3 Cathy Smith
Line 4 Matthew Nerk
Line 5 Janine Nerk
Line 6 Marion Martin
Line 7 Louise Nerk
Line 8 Melissa Nerk
Line 9 Kim Nerk
Line 10 Luke Nerk
Line 11 Greg Smith
Line 12 Marta Smith
Line 13 Isaac Smith
Line 14 Eliza Nerk
Line 15 Henry Nerk
Line 16 Karina Nerk

Please type in line number for whom you'd like to see the family members:
0

You've selected to see the family members of: Sam Nerk
Traceback (most recent call last):
  File "U:\Subject\11\SDD\Family tree\FamilyTree.py", line 46, in <module>
    if child == ID[x]: #n will store the line on which the child is
IndexError: string index out of range

我不知道为什么说索引超出范围,它应该在 x = 7 时跳出 while 循环(因为子 ID 在第 7 行中应该相等)。 请原谅我的业余代码,我真的刚刚开始学习 python,任何帮助我都会收到此错误的原因将不胜感激:)

这一行打破了你的逻辑:

#Finds the line on which the ID is located in ParentChild.txt
ID = ID[i]

您应该将此值分配给一个唯一的变量名称,因为它已用于在程序的前面声明一个 Id 数组。

您实际上是在覆盖它的值列表。

#Finds the line on which the ID is located in ParentChild.txt
unique_ID = ID[i]

尝试像上面的示例一样重命名它,并重命名对该变量的任何其他引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM