[英]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.