简体   繁体   中英

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

So I'm making this family tree program thing, I've got two files:

FamNames.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

and 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 stores the Lastname, Firstname, Gender, ID ParentChild store the ID of parent, ID of child

Heres my code for finding the child of anyone the user wants:

#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])

Heres the output/error:

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

I don't know why says the index is out of range, it should be breaking out of the while loop once x = 7 (as the child Id should be equal in line 7). Please excuse my amateurish code, I literally just started learning python, any help would be appreciated on why I'm getting this error:)

This line is breaking your logic:

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

You should assign this value to a unique variable name as it's already used to declare an array of Id's earlier in the program.

You are essentially overwriting it's list of values.

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

Try renaming it like above example and renaming any other reference to the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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