这是我的项目请理解我是一个初学者。
问题:每次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()
2条答案
按热度按时间zwghvu4y1#
break
语句breakonly最接近for / whille
。举个例子:
oyxsuwqo2#
你可以试试这个-