航班座位:Python(重置座位问题)

2024-10-02 20:43:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一项任务,在航班上预订座位,到目前为止,我的代码适用于除航班完全预订时重置所有可用座位之外的所有事情

NUM_ROWS = 2
NUM_COLS = 1
AVAIL = '-'
BOOKED = 'X'

seatTable = []
for i in range(NUM_ROWS):
    column = []
    for j in range(NUM_COLS):
        column.append(AVAIL)
    seatTable.append(column)

def resetTable(seats):          ##<--need to fix
    seatTable = []
    for i in range(NUM_ROWS):
        column = []
        for j in range(NUM_COLS):
            column.append(AVAIL)
        seatTable.append(column)
    resetTable(printTable(seatTable))   ##<-- not correct
    print()

def printTable(seats):
    i=1
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    print('Row', end=' ')

    for num in range(NUM_COLS):
        print(f'{alpha[num]:2s}'.format(alpha),end='')
    print()

    for num in seats:
        print(f'{str(i):3s}'.format(str(i)), end=' ')
        i+=1
        for j in num:
            print(j,end=' ')
        print()

def full(seats):
    for row in seats:
        for seat in row:
            if seat == AVAIL:
                return False
    return True

def getRes():
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    while True:
        try:
            rowNum = input(f'Enter a row number (1 to {NUM_ROWS}): ')
            seatLetter = input(f'Enter a seat letter (A to {(alpha[NUM_COLS-1]).upper()}): ')
            reserve(seatTable,rowNum,seatLetter)
            break
        except:
            pass
        print('Error, Please choose another seat')
    print(f'Seat {rowNum}{seatLetter.upper()} has been booked\n')

def reserve(seats,resR,resC):
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    column = 0
    p = 0
    for i in alpha:
        if i.lower() == resC.lower():
            column = p
        p+=1
    row = int(resR)-1
    seats[row][column] = BOOKED

def main():
    printTable(seatTable)
    while not full(seatTable):
        getRes()
        printTable(seatTable)
    print('Plane is booked')
    next = input('\nWould you like to check the next flight? (Y/N): ')
    if next == 'y':                        ##<---problem 
        resetTable(printTable(seatTable))
        return main()
    else:
        exit()
main()

当我运行代码检查“下一次航班”时,座椅重置,但随后卡在无限循环上,随后出现错误:

line 23, in resetTable
     resetTable(printTable(seatTable)) ##<-- not correct
     [Previous line repeated 992 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

如何修复resetTable()函数以使其正常工作


Tags: inalphafordefrangecolumnnumrows
1条回答
网友
1楼 · 发布于 2024-10-02 20:43:35

试试这个

NUM_ROWS = 2
NUM_COLS = 1
AVAIL = '-'
BOOKED = 'X'

seatTable = []
for i in range(NUM_ROWS):
    column = []
    for j in range(NUM_COLS):
        column.append(AVAIL)
    seatTable.append(column)

def resetTable(seats): 
    for i in range(NUM_ROWS):
        column = []
        for j in range(NUM_COLS):
            seatTable[i][j] = AVAIL

def printTable(seats):
    i=1
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    print('Row', end=' ')

    for num in range(NUM_COLS):
        print(f'{alpha[num]:2s}'.format(alpha),end='')
    print()

    for num in seats:
        print(f'{str(i):3s}'.format(str(i)), end=' ')
        i+=1
        for j in num:
            print(j,end=' ')
        print()

def full(seats):
    for row in seats:
        for seat in row:
            if seat == AVAIL:
                return False
    return True

def getRes():
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    while True:
        try:
            rowNum = input(f'Enter a row number (1 to {NUM_ROWS}): ')
            seatLetter = input(f'Enter a seat letter (A to {(alpha[NUM_COLS-1]).upper()}): ')
            reserve(seatTable,rowNum,seatLetter)
            break
        except:
            pass
        print('Error, Please choose another seat')
    print(f'Seat {rowNum}{seatLetter.upper()} has been booked\n')

def reserve(seats,resR,resC):
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    column = 0
    p = 0
    for i in alpha:
        if i.lower() == resC.lower():
            column = p
        p+=1
    row = int(resR)-1
    seats[row][column] = BOOKED

def main():
    printTable(seatTable)
    while not full(seatTable):
        getRes()
        printTable(seatTable)
    print('Plane is booked')
    next = input('\nWould you like to check the next flight? (Y/N): ')
    if next == 'y': 
        resetTable(seatTable)
        return main()
    else:
        exit()
main()

相关问题 更多 >