Python初学者-重复LINE或FOR循环

ryoqjall  于 2023-05-08  发布在  Python
关注(0)|答案(2)|浏览(134)

这是我的项目请理解我是一个初学者。
问题:每次user_inputYbreakN时,我必须从这行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()
zwghvu4y

zwghvu4y1#

break语句breakonly最接近for / whille
举个例子:

n = 6
while n > 4:
    print(">>>>>> n:", n)
    n-=1
    for i in range(3):
        print(">>> i:", i)
        for x in range(2):
            print("x:", x)
            if x == 1:
                print("BREAK_____ inner for")
                break
>>>>>> n: 6
>>> i: 0
x: 0
x: 1
BREAK_____ inner for
>>> i: 1
x: 0
x: 1
BREAK_____ inner for
>>> i: 2
x: 0
x: 1
BREAK_____ inner for
>>>>>> n: 5
>>> i: 0
x: 0
x: 1
BREAK_____ inner for
>>> i: 1
x: 0
x: 1
BREAK_____ inner for
>>> i: 2
x: 0
x: 1
BREAK_____ inner for
oyxsuwqo

oyxsuwqo2#

你可以试试这个-

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 == 'n':
                exit()
            else:
                while q.lower() == 'y' :
                    for k, v in d.items() :
                    if addprod == k[1] :
                        print('Added:', k, v)
                    q = input('Add more? y/n')

相关问题