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
2
  • 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? Commented Jul 24, 2021 at 14:17
  • I can't understand. What are the "two values" mentioned in the title? Commented Oct 11, 2022 at 3:31

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Inheriting from list is problematic. What about list.extend, etc...? Another approach is outlined in this answer: stackoverflow.com/a/3488283/642070
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)

6 Comments

Was not aware that all other instances will be referencing the same default list. Should be fixed now though.
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.
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!
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.
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.
|
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.