如何保存用户输入值?



我必须为我的高中项目制作一个书店数据库模型。 有输入新书信息的功能,但我希望即使在杀死程序后它也能保留。目前,我刚刚在代码本身中输入了几本书,以便在运行时它不会为空。

我差不多完成了,但我只是想知道如何保存用户输入数据以供以后使用。

books={1001:['Inferno','Dan Brown','Anchor Books','Thriller',42.00,70],
1002:['As You Like It','William Shakespear','Penguin Publications','Classics',20.00,54],
1003:['The Kite Runner','Khaled Hosseini','Bloomsbury Publcations','Drama',30.00,70],
1004:['A Thousand Splendid Suns','Khaled Hosseini','Bloomsbury Publications','Fiction',35.00,70],
1005:['The Girl on The Train','Paula Hawkins','Riverhead Books','Thriller',28.00,100],
1006:['The Alchemist','Paulo Coelho','Rupa Books','Drama',25.00,50]}
while True:
print ('ID    |   Name')
[print(key,' | ',books[key][0]) for key in books.keys()]
print (
'''
MAIN MENU:-
1. Add new book details
2. Modify data of a specific book
3. Delete all data of a book
4. View all data of a book
***********************************
5. Check Availability
6. Sales
7. Exit
''')
cho=int(input('Enter your choice(1-7):'))
if cho==1:
book_id=int(input('Enter the book id:'))
b_name=input('Enter the book name:-')
a_name=input('Enter the authors name:')
p_name=input('Enter name of publication:')
genre=input('Enter the genre of the book:')
price=float(input('Enter the price of the book (with decimal place):'))
no_copy=int(input('Enter no of copies available:'))
books[book_id]=[b_name,a_name,p_name,genre,price,no_copy]
elif cho==2:
book_id=int(input('Enter book id:'))
print (books[book_id])
print('''0. Book name
1. Author's name
2. Publisher
3. Genre
4. Price
5. No of copies''')
index=int(input('Enter index of data to be changed:'))
if index>=0 and index<=3:
new_val=input('Enter the modified value:')
elif index==4:
new_val=float(input('Enter the modified value (with decimal place):'))
elif index==5:
new_val=int(input('Enter the modified value:'))
books[book_id][index]=new_val
elif cho==3:
book_id=int(input('Enter book id of book to be deleted:'))
print ('Book', book_id, 'data deleted')
del books[book_id]
elif cho==4:
book_id=int(input('Enter the book id:'))
print(books[book_id])
elif cho==5:
book_id=int(input('Enter book id of book to check availability:'))
print ('''
''', books[book_id][5], 'copies are currently in stock')
elif cho==6:
book_id=int(input('Enter book id of book to buy:'))
nos=int(input('Number of copies to buy'))
books[book_id][5]=books[book_id][5]-nos
print (nos,'copies of books purchased')
elif cho==7:
break

如果代码看起来很痛,我很抱歉,我对 Python 非常陌生。另外,如果您有任何建议可以使其更好,那就太棒了。

使用 csv 文件来存储所有数据。

import csv   
if cho==1:
book_id=int(input('Enter the book id:'))
b_name=input('Enter the book name:-')
a_name=input('Enter the authors name:')
p_name=input('Enter name of publication:')
genre=input('Enter the genre of the book:')
price=float(input('Enter the price of the book (with decimal place):'))
no_copy=int(input('Enter no of copies available:'))
books[book_id]=[b_name,a_name,p_name,genre,price,no_copy]
for book in books:
with open('books.csv', 'a') as bks:
writer = csv.writer(bks)
writer.writerow(book)

您可以将输入写入文件(根据您的情况最好是 csv 文件(。CSV 文件 I/O 链接

您可以将变量保存/加载到文件中。

一个选项是"打嗝">

要安装:

pip install hickle

用:

import hickle as hkl
# write variables to filename [a,b,c can be of any size]
hkl.dump([a,b,c], filename)
# load variables from filename
a,b,c = hkl.load(filename)

修改了您的代码:

import hickle as hkl
filename='books.file'
books ={}
try:
books = hkl.load(filename)
except IOError:
print("File not exists")

while True:
print ('ID    |   Name')
[print(key,' | ',books[key][0]) for key in books.keys()]
print (
'''
MAIN MENU:-
1. Add new book details
2. Modify data of a specific book
3. Delete all data of a book
4. View all data of a book
***********************************
5. Check Availability
6. Sales
7. Exit
''')
cho=int(input('Enter your choice(1-7):'))
if cho==1:
book_id=int(input('Enter the book id:'))
b_name=input('Enter the book name:-')
a_name=input('Enter the authors name:')
p_name=input('Enter name of publication:')
genre=input('Enter the genre of the book:')
price=float(input('Enter the price of the book (with decimal place):'))
no_copy=int(input('Enter no of copies available:'))
books[book_id]=[b_name,a_name,p_name,genre,price,no_copy]
hkl.dump(books, filename)
elif cho==2:
book_id=int(input('Enter book id:'))
print (books[book_id])
print('''0. Book name
1. Author's name
2. Publisher
3. Genre
4. Price
5. No of copies''')
index=int(input('Enter index of data to be changed:'))
if index>=0 and index<=3:
new_val=input('Enter the modified value:')
elif index==4:
new_val=float(input('Enter the modified value (with decimal place):'))
elif index==5:
new_val=int(input('Enter the modified value:'))
books[book_id][index]=new_val
elif cho==3:
book_id=int(input('Enter book id of book to be deleted:'))
print ('Book', book_id, 'data deleted')
del books[book_id]
elif cho==4:
book_id=int(input('Enter the book id:'))
print(books[book_id])
elif cho==5:
book_id=int(input('Enter book id of book to check availability:'))
print ('''
''', books[book_id][5], 'copies are currently in stock')
elif cho==6:
book_id=int(input('Enter book id of book to buy:'))
nos=int(input('Number of copies to buy'))
books[book_id][5]=books[book_id][5]-nos
print (nos,'copies of books purchased')
elif cho==7:
break

有几种方法可以解决这个问题:

  1. 将程序连接到数据库。我会说在这种情况下最合适的是SQLite。
  2. 将数据本地保存到文件中。我建议使用熊猫将数据转换为数据框并对其进行操作。之后,您可以将数据框保存为 CSV。以下是熊猫的文档。

至于改进你的代码,我建议把它分解成函数。调试和理解会更容易。祝你的项目好运!

相关内容

  • 没有找到相关文章

最新更新