В моей программе я читаю из файла, помещаю данные в список и редактирую указанные данные, затем редактирую исходный файл с любыми данными, которые были изменены.Программа отредактирует первые элементы (имена) и запишет их правильно, но когда дело доходит до данных второго типа (пола), она отредактирует их внутри списка, напечатает, что они были изменены в списке, но не запишетв файл.
name = input("Which character do you want to edit? ")
lineName = name
j = findLinesToCharacter(lineName)
#Counts the number of lines to an existing name
which is entered and set as a sentinel value, or lineName
if change == "name":
newName = input("What would you like their new name to be?")
i = 0
while i in range(len(existingCharacterList)):
if existingCharacterList[i].getName() == name:
existingCharacterList[i].changeName(newName)
print("The character is now", format(existingCharacterList[i].getName(),
replaceLine(j, existingCharacterList[i].getName())
# Calls a function that replaces the data in a line in the original file
#as counted by findLinesToCharacter(lineName)
else:
i += 1
#The above for changing a name works fine,
#except for the very first name in the list.
#The next part is where the problem is.
#It should do the same thing in the next section
#except add one to j since all genders are added
#one line below the name. In the print output it
#shows that the gender has been changed in the list,
#but when I go to the file or try to print a table
#of the file using a different function in the program,
#its like it never happened.
elif change == "gender":
newGender = input("What gender is the character? ")
i = 0
for i in range(len(existingCharacterList)):
if existingCharacterList[i].getName() == name:
existingCharacterList[i].changeGender(newGender)
print("The character is now", format(existingCharacterList[i].getName(),
"25s"),format(existingCharacterList[i].getGender()
replaceLine((j+1),existingCharacterList[i].getGender())
#This is the function that writes to the specific line number
def replaceLine(lineNumber, text):
fileName = "Character List.txt"
linesRead = open(fileName, 'r').readlines()
linesRead[lineNumber] = (text + "\n")
out = open(fileName, 'w')
out.writelines(linesRead)
out.close()