【发布时间】:2020-12-02 17:23:08
【问题描述】:
我正在创建一个项目,其中我有一个登录系统(非常基本),然后我需要创建具有特定名称的不同 txt 文件,例如:T1、T2、T3... 然后可以访问每个 txt文件并重置其中的数据。
这基本上是一个餐厅软件,其中用户使用名称登录,然后继续为某个表号创建文件。在那个 txt 文件中,我计划将账单保存为字符串代码和附加的价格。
到目前为止,我知道如果您open() 一个不存在的文件,它将被创建。
while True:
action = input ('''
- NEW table
\n - ADD table
\n - BILL
\n - ... ''')
if action.lower() == 'new':
t = open('T.txt', 'w')
elif action.lower() == 'add':
table = input ('Select desired table number')
elif action.lower() == "exit":
exit()
第二个elif 是我计划使用用户输入选择所需文件的地方。
完整代码:
with open('names.txt', 'r') as r :
f_n = r.read().splitlines()
print("Welcome to NAME.app")
##############
# USER LOGIN #
##############
while True:
name = input("""
\n - Insert name to logg in
\n - ADD to save new user
\n - LIST to see saved users
\n - REMOVE to delete a user
\n - EXIT to finish
\n - ...""")
lname = name.lower()
if lname == "add":
n_input = input("Name:")
with open('names.txt', 'a') as f:
f.write(n_input + '\n')
elif lname == "list":
with open('names.txt') as f:
print(f.read().splitlines())
f.close()
elif name in f_n:
print("Logged as", name.upper())
input('Welcome, press enter to continue \n')
break
elif lname == 'remove':
rem = input("Insert user name to remove \n ...")
with open('names.txt', 'r+') as f:
l = f.readlines()
l = [z for z in l if rem not in z]
with open('names.txt', 'w') as f:
f.writelines(l)
elif lname == "exit":
exit()
####################
# TABLE MANAGEMENT #
####################
while True:
action = input ('''
- NEW table
\n - ADD table
\n - BILL
\n - ... ''')
if action.lower() == 'new':
t = open('T.txt', 'w')
elif action.lower() == 'add':
table = input ('Select desired table number')
elif action.lower() == "exit":
exit()
谢谢。
【问题讨论】:
标签: python python-3.x data-structures