code is given below. when I'm adding more items after running in loop it takes only first item in file and rest not. so please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

code is given below. when I'm adding more items after running in loop it takes only first item in file and rest not. so please

code is given below. when I'm adding more items after running in loop it takes only first item in file and rest not. so please help me. thank you import datetime def gettime(): return datetime.datetime.now() print("CUSTOMER RATION BORROWING MANAGEMENT") print("what do you want?\n 1. Log \n2.Retrieve") d = int(input()) if (d==1): name = (input("Enter customer name\n----")) while (True): grocerry = str(input("items\n----")) price = (input("enter price\n----")) a = str(input("Want to add more items?\ny for Yes -- n for No\n")) if (a=="y"): continue else: break with open (f"{name}.txt","a",) as name: name.write (str([str(gettime())])+" :- "+grocerry+":- "+str(price)+"\n") print("Noted successfully.....") else: name = str(input("Enter customer name\n")) with open (f"{name}.txt") as name: for i in name: print(i,end="")

30th Jan 2022, 7:13 AM
Sheikh Mohammed Ahmedraza
Sheikh Mohammed Ahmedraza - avatar
2 Answers
+ 3
In your snippet of code here: if (d==1): name = (input("Enter customer name\n----")) namefile = open("{}.txt".format(name), "a") # Open the file first while (True): grocerry = str(input("items\n----")) price = (input("enter price\n----")) namefile.write(str([str(gettime())])+" :- "+grocerry+":- "+str(price)+"\n") # After adding each item, you want to write it into the file a = str(input("Want to add more items?\ny for Yes -- n for No\n")) if (a=="y"): continue else: break You can get rid of this section: with open (f"{name}.txt","a",) as name: name.write (str([str(gettime())])+" :- "+grocerry+":- "+str(price)+"\n") And finally, when you reopen the file for retrieval: name = str(input("Enter customer name\n")) with open (f"{name}.txt") as name: for i in name.readlines(): # This will allow you to read all the lines print(i,end="")
30th Jan 2022, 7:58 AM
John Leung
+ 1
thank you John
30th Jan 2022, 8:38 AM
Sheikh Mohammed Ahmedraza
Sheikh Mohammed Ahmedraza - avatar