Hi I am fairly new to python. I want to do a seating reservation program containing 32 seats so the users can reserve seats and unreserve seats. I know I need a class that sort of contains the seats, but I dont know how to make it....

I have made a menu so that the user can choose to reserve seats and unreserve seats.

the seats should be looking similar like below and when someone reserves a seat it will be marked with *. example like this: *17*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

I also want this to be saved into a text file, so it's impossible to double book seats,

can anyone help me?

class Seats(object):

    def __init__(self,nr=32): #FIXME arbitrary number of seats
        self._seats=list(range(1,nr+1))
        self._reserved=set()
    
    def reserve(self,index):
        #TODO raise exception when invalid index
        self._reserved.add(index)
        
    def unreserve(self,index):
        #TODO raise exception when invalid index
        self._reserved.remove(index)
    
    def save(self):
        #TODO
        pass
        
    def load(self):
        #TODO
        pass
        
    def __str__(self):
        out=""
        for i in self._seats:
            if i in self._reserved:
                out+="*"+str(i)+"* "
            else:
                out+=str(i)+" "
        return out

if __name__=="__main__":
    s=Seats()
    print s
    s.reserve(3)
    print s
    s.reserve(1)
    print s
    s.unreserve(1)
    print s

Edited 6 Years Ago by slate: n/a

Ok thank you! How do I do, if I want to prevent double booking ? should I do an if statement for that?

example: if * in index:
print(" Seat already booked")
else:
self._reserved.add(index)

Ok thank you! How do I do, if I want to prevent double booking ? should I do an if statement for that?

example: if * in index:
print(" Seat already booked")
else:
self._reserved.add(index)

class AlreadyReserved(Exception): pass
class InvalidSeat(Exception):pass

class Seats(object):

    def __init__(self,nr=32): #FIXME arbitrary number of seats
        self._seats=list(range(1,nr+1))
        self._reserved=set()
    
    def reserve(self,index):
	if index> len(self._seats) or index<1:
		raise InvalidSeat
	if index in self._reserved:
		raise AlreadyReserved
        self._reserved.add(index)
        
    def unreserve(self,index):
        #TODO raise exception when invalid index
        self._reserved.remove(index)
    
    def save(self):
        #TODO
        pass
        
    def load(self):
        #TODO
        pass
        
    def __str__(self):
        out=""
        for i in self._seats:
            if i in self._reserved:
                out+="*"+str(i)+"* "
            else:
                out+=str(i)+" "
        return out

if __name__=="__main__":
    s=Seats()
    print s
    s.reserve(3)
    print s
    s.reserve(1)
    print s
    try:
       s.reserve(1)
    except AlreadyReserved:
       print "sorry, seat 1 is already reserved"
    print s
    s.unreserve(1)
    print s

Ok , but I get this error then.. inconsistent use of tabs and spaces in indentation python

You need to edit the indents because there are some tabs and some spaces, and they are not all aligned. My advice would be to delete the whitespace on each line and then tab it out the appropriate number of times. Using tab is alot quicker and easier than using spaces.

Still it is ill adviced to use tabs in writing code, use autoindenting editor with whitespace tabs. Those have function 'untabbify', use that and correct the indention with whitespace tabs.

I don't get what is wrong with using tabs. It makes sure everything is aligned evenly and is very quick compared to space bar...

And I do not get the problem with my code.
If I click toggle plain text, copy the code to the clipboard and paste it into a notepad, then run it. It works.

Edited 6 Years Ago by slate: n/a

This question has already been answered. Start a new discussion instead.