В основном, что я хочу сделать, так это показать список заметок в python, чтобы пользователь мог видеть, что находится в списке, не открывая файл. Я сделал это. Теперь я хочу, чтобы пользователь вводил желаемый продукт и сохранял его, а также распечатывал продукт и цену. Это то, что текстовый файл выглядит следующим образом:Сбор пользовательского ввода. Чтение и запись в текстовые файлы в python
000,67463528,50mm bolts,2.20
001,34512340,plain brackets,0.50
002,56756777,100mm bolts,0.20
003,90673412,l-shaped brackets,1.20
004,45378928,normal brackets,0.80
005,1638647,10mm bolts,0.10
006,14372841,plain brackets,1.29
007,29384754,200mm bolts,0.50
008,12345768,screwdriver,10.99
До сих пор я могу добавить продукты в конце кода, и я могу вставить текст в определенный номер строки. Я разрабатываю, как распечатывать квитанцию на основе ввода пользователем. Я новичок в python, рассматривая это как хобби, любая помощь была бы весьма признательна. Благодаря!
Это код питона:
def search():
gtin = input("What is the GTIN code? ")
des = input("What is the name of the product? ")
price = input ("What is the price of the product? ")
position_str = input("What line do you want this inserted at? ")
print("This is the list now: ")
position = int(position_str)
with open("task2.txt", "r") as a_file:
data = a_file.readlines()
data.insert(position, "00" + position_str + "," + gtin + "," + des + "," + price + "\n")
data = "".join(data)
with open("task2.txt", "w") as a_file:
a_file.write(data)
input()
with open('task2.txt','r')as Task2:
print('{0:<19} {1:<19} {2:<19} {3:<19}'.format("\nNo.","GTIN-8","Item Name","Price"))
for row in Task2:
row=row.strip()
eachItem=row.split(",")
print('{0:<19} {1:<19} {2:<19} {3:<19}'.format(eachItem[0],eachItem[1],eachItem[2],eachItem[3]))
print()
def add():
print("The data you put here will be added onto the end ")
gtin = input("What is the GTIN-8? ")
des = input("What is the description? ")
price = input("What is the price? ") #gets all the info they want to add
print("This is the list now: ")
with open("task2.txt","a") as a_file:
a_file.writelines(" ," + gtin + "," + des + "," + price + "\n")
a_file.close()
print("Product has been added")
with open('task2.txt','r')as Task2:
print('{0:<19} {1:<19} {2:<19} {3:<19}'.format("\nNo.","GTIN-8","Item Name","Price"))
for row in Task2:
row=row.strip()
eachItem=row.split(",")
print('{0:<19} {1:<19} {2:<19} {3:<19}'.format(eachItem[0],eachItem[1],eachItem[2],eachItem[3]))
print()
def reciept():
print()
#this is the menu. this is where the user will make choices on what they do
def menu():
print("What would you like to do?")
with open('task2.txt','r')as Task2:
print('{0:<19} {1:<19} {2:<19} {3:<19}'.format("\nNo.","GTIN-8","Item Name","Price"))
for row in Task2:
row=row.strip()
eachItem=row.split(",")
print('{0:<19} {1:<19} {2:<19} {3:<19}'.format(eachItem[0],eachItem[1],eachItem[2],eachItem[3]))
print()
print("1. Add a product onto a specific line number?")
print("2. Or add a product onto the end?")
print("3. Buy products and have them printed onto a reciept?")
choice=int(input("Which one?: "))
if choice==1:
search()
elif choice==2:
add()
elif choice==3:
reciept()
menu()
Если это ответили на ваш вопрос, пожалуйста, примите ответ. – SirJames