0

I have a program that simulates a bus in the form of a list and there is an ability to add passengers to the bus. I want to be able to set a max number of passengers, so that if the list exceeds 25 passengers I display a code stating that the bus is full.

Is it possible to set this limit in a list with Python.

Here is a snippet of the code:


#defining a class for the passenger list
class Bus:
  passengers = []
  number_of_passengers = 0
  • 1
    If you are writing a class, it seems more natural to put the limit as an attribute of the class instead of a 2 member list. Any reason why a list is used instead? – tdelaney Jul 24 '21 at 14:17
  • I can't understand. What are the "two values" mentioned in the title? – Karl Knechtel Oct 11 '22 at 03:31

4 Answers4

3

You can use super keyword for override lists.

class PassengerList(list):
    limit = 0
    def __init__(self, lim):
        self.limit = lim

    def append(self, item):
        if len(self) >= self.limit:
            raise Exception('Limit exceeded.')
        super(PassengerList, self).append(item)  

passengers = PassengerList(25)
passengers.append('abc')

You can set limit by parameter.

Onurgule
  • 707
  • 8
  • 21
  • Inheriting from list is problematic. What about list.extend, etc...? Another approach is outlined in this answer: https://stackoverflow.com/a/3488283/642070 – tdelaney Jul 24 '21 at 15:57
1

You'd probably want to check the length of the list and then decide if you'll add a passenger or display a message. Something like so:

class Bus:

  def __init__(self, max_number_of_passengers = 25):
      self.passengers = []
      self.max_number_of_passengers = max_number_of_passengers

  def add_passenger(self, passenger):
      if len(self.passengers) > self.max_number_of_passengers:
         # display message
      else: 
         self.passengers.append(passenger)
haidousm
  • 556
  • 6
  • 21
  • 1
    Was not aware that all other instances will be referencing the same default list. Should be fixed now though. – haidousm Jul 24 '21 at 14:23
  • 1
    I like the edit except I don't see a need to make passengers a parameter. You could just always do `self.passengers = []`. The instance starts with no passengers and you add them via the method. – tdelaney Jul 24 '21 at 14:26
  • That's a fair point, I was going off the assumption that they'd like to initialize a bus with an already-existing list of passengers although that is unlikely. Edited my answer to reflect that change as well. Thank you! – haidousm Jul 24 '21 at 14:29
  • 1
    When python compiles a function, it adds the default parameters to the function object. Each time the function is called, those default parameters are used. So, for a mutable object like a list, a single list is created, added to the function object and used as the default every time. The same list ends up being used by everybody. – tdelaney Jul 24 '21 at 14:29
  • Oh wow, never knew that. Thanks for letting me know, I'm pretty sure that would've cost me hours of debugging at some point in my life. – haidousm Jul 24 '21 at 14:31
  • Yeah, this one bites everybody once. – tdelaney Jul 24 '21 at 15:59
0
class Bus:
   
   def __init__(self, limit=25):
       self.passengers = []
       self.bus_limit = limit
       self.is_bus_full = False

   def add_passenger(self):
       if len(self.passengers) < self.bus_limit:
           self.passengers.append(1) # add dummy values
       else:
           self.is_bus_full = True
   
   def passenger_left(self):
       self.passengers.pop()

   def bus_status(self):
       if self.is_bus_full:
           return 'This bus is full'
       else:
           return 'This bus has vacant seats'
 

You can write something like this.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

you can use a class

class Bus:
    def __init__(self):
        self.passengers = []
        self.MAX_LIMIT = 25
        
    def add_passenger(self, passenger):
        if(len(self.passengers) <= self.MAX_LIMIT):
            self.passengers.append(passenger)
        else:
            print('sorry bus is full')
            
    def show_passengers(self):
        print(self.passengers)
        

bus = Bus()
for i in range(26):
    bus.add_passenger(i)
    
bus.add_passenger(26) #sorry bus is full
Epsi95
  • 8,832
  • 1
  • 16
  • 34