i am still new to python, i have tried this way to overwrite a line on a txt file
'''
answer =input("R/S/L/M:")
if answer == "R":
print ("Racer")
f=open('character.txt', 'w+')
with open("character.txt") as file_in:
lines = [1]
for line in file_in:
lines.append(line)
amount=int(input("please enter the amount"))
f.write(answer )
f.write("" + "-" + str(amount) + '\n\n')
f.write("" + '\n')
f.close()
elif answer == "S":
print ("Sage")
f=open('character.txt', 'w+')
with open("character.txt") as file_in:
lines = [3]
for line in file_in:
lines.append(line)
amount=int(input("please enter the amount"))
f.write(answer )
f.write("" + "-" + str(amount) + '\n\n')
f.write("" + '\n')
f.close()
'''
it replaces all of the lines of text regardless on the txt file what i am trying to do is so when i select R it writes to the 1st line on the txt file, and when i select S it writes to the 3rd line on the txt file
i have tried now
'''
if answer == "R":
print ("Racer")
f=open('character.txt', 'w+')
with open("character.txt", "r+") as myfile:
data = myfile.read()
myfile.seek(0)
amount=int(input("please enter the amount"))
newData1=(answer + "" + "-" + str(amount) + '\n\n')
myfile.write(newData1)
myfile.truncate()
f.close()
elif answer == "S":
print ("Sage")
f=open('character.txt', 'w+')
with open("character.txt", "r+") as myfile:
data = myfile.read(4)
myfile.seek(4)
amount=int(input("please enter the amount"))
newData2=(answer + "" + "-" + str(amount) + '\n\n')
myfile.write(newData)
myfile.truncate(4)
f.close()
'''
can someone please point me in the right direction