I am trying to change some information in a textfile. Initially, the textfile looks like
Entertainment
70
Clothing
15
Food
25
after running this program
import os, sys
import cs50
from cs50 import get_int
def main():
print("Welcome to the money tracker program!")
SpentOnEntertainment = get_int("How much money do you plan on spending on Entertainment? ")
SpentOnClothing = get_int("How much money do you plan on spending on Clothing? ")
SpentOnFood = get_int("How much money do you plan on spending on Food? ")
Textfile = open("AFile2.txt", "r+")
currentfunds = Textfile.readlines()
if(SpentOnEntertainment > int(currentfunds[1])):
print("Sorry, you can only spend", currentfunds[1], "dollars on Entertainment.")
SpentOnEntertainment = int(currentfunds[1])
currentfunds[1] = str(int(currentfunds[1]) - SpentOnEntertainment) + "\n"
if(SpentOnClothing > int(currentfunds[3])):
print("Sorry, you can only spend", currentfunds[3], "dollars on Clothing.")
SpentOnClothing = int(currentfunds[3])
currentfunds[3] = str(int(currentfunds[3]) - SpentOnClothing) + "\n"
if(SpentOnFood > int(currentfunds[5])):
print("Sorry, you can only spend", currentfunds[5], "dollars on Food.")
SpentOnFood = int(currentfunds[5])
currentfunds[5] = str(int(currentfunds[5]) - SpentOnFood) + "\n"
Textfile.writelines(currentfunds)
sys.exit(0)
if(__name__ == "__main__"):
main()
which allows the user to choose how much money they want to spend and thus change the remaining money, the new lines are appended to the end of my original lines, and my end result (the text file) is
70
Clothing
15
Food
25Entertainment
25
Clothing
5
Food
0
How can I get rid of the first 6 lines as they are no longer relevant? I tried using methods such as deleting items [0:6] after rereading the list but nothing works.