Rewriting Certain parts of a text file (dynamically)

Viewed 50

I have a text file like this

A,OXXX#XXXX#XXOO
B,OOXX#XXXX#XXOO
C,OXXX#XXXO#OXXO
D,OOXX#XXXX#XXOO
E,OOOX#OXXO#XXOO
F,##XX#OXXX#XO##

Its something like a movie cinema Where O is available seats, X is unavailable seats and # is a wall

bookSeat = input('Enter seats to book: ').split(',')
    for booking in bookSeat:
        bookRow = booking[0]
        bookColumn = booking[1:]
        availableSeats = seatingPlan[bookRow][int(bookColumn)-1]
            if availableSeats == 'O':
                f = open('Monkey Goes East-202209081430.txt','w')
                #idk whats the next step
            elif availableSeats == 'X':
                print("Seats are already booked!')
            elif availableSeats == '#':
                print('These seats are unavailable')

So if i input A1,B14, it will update the text file into

A,**X**XXX#XXXX#XXOO
B,OOXX#XXXX#XXO**X**
C,OXXX#XXXO#OXXO
D,OOXX#XXXX#XXOO
E,OOOX#OXXO#XXOO
F,##XX#OXXX#XO##

Bolded to show its been change

please help

1 Answers

assuming seatingPlan is a python dict like {'A': ['X','#','O', ...]} and assuming you can modify seatingPlan you could do something along the lines of:

this is pseudo-code. i did not test it.

bookSeat = input('Enter seats to book: ').split(',')
isFullyBookable = True

for booking in bookSeat:
    bookRow = booking[0]
    bookColumn = booking[1:]
    availableSeats = seatingPlan[bookRow][int(bookColumn)-1]
    
    if availableSeats == 'O':
        seatingPlan[bookRow][int(bookColumn)-1] = 'X'
    elif availableSeats == 'X':
        print('Seats are already booked!')
        isFullyBookable = False
    elif availableSeats == '#':
        print('These seats are unavailable')
        isFullyBookable = False
    # further checking logic ....

if isFullyBookable:
    with open('Monkey Goes East-202209081430.txt','w') as f:
        for rowCode in sorted(seatingPlan.keys()):
            f.write(rowCode + ',' + "".join(seatingPlan[rowCode]) + '\n')
Related