【发布时间】:2020-12-07 11:01:17
【问题描述】:
这是我的项目。请理解我是初学者。
问题:每次 user_input 是 Y 并且 break 每次都是 N 时,我都必须从这一行重复 for k, v in d.items() : 。我不知道我是否可以从行号或for 或上一行的while 重复。
也许我可以在for 之前def 或者有任何方式从任何你想要的地方重复。
感谢您的阅读。
代码:
elif action.lower() == 'add':
table = input ('Select desired table number: \n - ...')
fulltab = 'T' + table
with open(fulltab + '.txt', 'w+') as f :
for k, v in d.items() :
print(k, v)
#print('Select codes: \n -...')
addprod = input('Insert order. \n - ...')
for k, v in d.items() :
if addprod == k[1] :
print('Added:', k, v)
q = input('Add more? y/n')
if q.lower() == 'y' : continue
if q.lower() == 'n' : break
完整代码以便更好地理解:
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 - ... ''')
d = {'(1) chburger': 19,'(2) bncburger': 23,'(3) plpasta': 6}
if action == 'new' :
tn = input('Insert table number \n - ...')
name = 'T' + tn
t = open(name + '.txt', 'w+')
print('Done')
elif action.lower() == 'add':
table = input ('Select desired table number: \n - ...')
fulltab = 'T' + table
with open(fulltab + '.txt', 'w+') as f :
for k, v in d.items() :
print(k, v)
#print('Select codes: \n -...')
addprod = input('Insert order. \n - ...')
for k, v in d.items() :
if addprod == k[1] :
print('Added:', k, v)
q = input('Add more? y/n')
if q.lower() == 'y' : continue
if q.lower() == 'n' : break
#File as F
elif action.lower() == 'bill' :
p_b = input('Please insert number of table. \n -... ')
with open (('T' + p_b)+ '.txt', 'r+') as p :
tobill = 0
for line in p : tobill = int(tobill) + int(line)
#print('Total to pay:', tobill)
xtra = input('Group table (+10 ppl)? y/n: \n')
if xtra == 'y' :
tobill = tobill + (tobill/100)*10
print('SERVICE CHARGE ADDED.')
elif xtra == 'n' : print ('Processing bill...')
print('Total to pay:', tobill)
elif action.lower() == "exit":
exit()
【问题讨论】:
-
如果您的 names.txt 中有“Someone”和“o`Som Gone”并删除名称“Som”......会发生什么?
-
在您的
àdd代码部分中,您打开了一个文件,但从未向其中写入任何内容 - 为什么? -
如果启动时没有names.txt会发生什么?
-
您可以简单地在
with open(fulltab + '.txt', 'w+') as f :之后添加另一个while True:并缩进处理输入一次的代码。如果用户随后给出了'n'的“q”,那么你就会中断。不知道你真正的问题是什么...... -
@PatrickArtner 1) 我只希望没有服务员/tress 登录,因为哈哈(这将是最后要修复的事情之一)。 2)我想第一行`with open`文件将被自动创建。 3)那是完美的,我想它会起作用的。我的问题是如何重复所有这些代码,并且有了这个答案,只要想一想我就知道它会起作用。感谢您抽出宝贵的时间帕特里克。添加代码部分不写任何东西,因为这是该行的最后一件事。但它会写入从
dictionary中选择的valuesfile.txt。很抱歉没有添加它。
标签: python python-3.x data-structures