I have encountered an error when writing my program and would like some help with it. The program has to replace a specific segment of the line with a calculated value. The segment in which I am talking about is the third index in a line, formatted like this:
Product01, 12346554, 15, 6
I am having the program calculate the value in which I want to replace with the final value but no matter how much I seem to try, my code frequently gives errors. For an example, I have used seek to try and move the cursor to allow myself to edit this value, however with the following code:
Total = 0
receipt = open("Receipt.txt", "w")
while True:
try:
Prod_Code = input("Enter a code or Done to get your final receipt: ")
if len(Prod_Code) == 8:
int(Prod_Code)
with open("Data Base.txt", "r+") as searchfile:
for line in searchfile:
if Prod_Code in line:
print(line)
Quantity = input("What quantity of this product do you want? ")
Total += float(line.split(",")[2]) * int(Quantity)
print(Quantity)
print(Total)
receipt.write(line)
print(line.split(",")[3])
W = int(line.split(",")[3]) - int(Quantity)
print(W)
L = (line)
L.seek(27, 0)
L.write(str(W))
elif Prod_Code == "Done":
receipt.close()
with open("Receipt.txt", "r") as datafile:
for item in datafile:
print(item.split(",")[1])
print(item.split(",")[2])
print("Your total cost is £", Total)
input("Press Enter to exit:")
exit()
else:
print("Incorrect length, try again")
except ValueError:
print("You must enter an integer")
However, when I run it this is the error that I get:
AttributeError: 'str' object has no attribute 'seek'.
I was wondering if anyone could help, and/or provide a different answer to my problem? Thanks in advance.