1

I want to create a bus station which stores some information of passengers.

And here is the class of passenger, the start_station means the begin place for the passenger, the destination_station means the aim place for the passenger.

class passenger():
    def __init__(self,name,start_station,destination_station):
        self.name = name
        self.start_station = start_station
        self.destination_station = destination_station

A1 = passenger("A1","A","B")
A2 = passenger("A2","A","C")
A3 = passenger("A3","A","D")
A4 = passenger("A4","A","D")

B1 = passenger("B1","B","C")
B2 = passenger("B2","B","C")
B3 = passenger("B3","B","C")

C1 = passenger("C1","C","D")
C2 = passenger("C2","C","A")

D1 = passenger("D1","D","B")
D2 = passenger("D2","D","C")
D3 = passenger("D3","D","A")

And the below code are the class of bus station. The passenger is the list,which stores the passengers who start from the station. The adjacent_bus_station is the dictionary which stores the adjacent stations and the distace to the station. The passenger_rate_flow is the number of passengers in the beginning.

from passenger import *
class Bus_Station():
    def __init__(self,name):
        self.name = name
        self.passenger = []
        self.adjacent_bus_station = {}
        self.passenger_rate_flow = 0

    def AddPassengers(self,*passengers):
        self.passenger.append(passengers)



    def AddAdjacent_bus_station(self):
        if(self.name == "A"):
            self.adjacent_bus_station["A<->B"] = 3
            self.adjacent_bus_station["A<->C"] = 7
            self.adjacent_bus_station["A<->D"] = 6

        if(self.name == "B"):
            self.adjacent_bus_station["B<->A"] = 3
            self.adjacent_bus_station["B<->C"] = 4
            self.adjacent_bus_station["B<->D"] = 5

        if (self.name == "C"):
            self.adjacent_bus_station["C<->A"] = 7
            self.adjacent_bus_station["C<->B"] = 4
            self.adjacent_bus_station["C<->D"] = 5

        if (self.name == "D"):
            self.adjacent_bus_station["D<->A"] = 6
            self.adjacent_bus_station["D<->B"] = 5
            self.adjacent_bus_station["D<->C"] = 5

    def Show_Info(self):
        for PG in self.passenger:

            info = "the name of passenger is "+ PG.name +"the start-station of passenger is "+PG.start_station+"the destination of passenger is "+ PG.destination_station
            print(info)

        for station,distance in self.adjacent_bus_station.items():
            print("the adjacent stations are " + station +" and the distance between the stations are "+str(distance))

        print("the rate of the passenger flow is "+str(self.passenger_rate_flow)+"\n\n\n\n")


    def PassengerRate(self):
        self.passenger_rate_flow = len(self.passenger)


A = Bus_Station("A")
B = Bus_Station("B")
C = Bus_Station("C")
D = Bus_Station("D")

A.AddAdjacent_bus_station()
A.AddPassengers(A1,A2,A3,A4)
A.PassengerRate()
A.Show_Info()

B.AddAdjacent_bus_station()
B.AddPassengers(B1,B2,B3)
B.PassengerRate()
B.Show_Info()

C.AddAdjacent_bus_station()
C.AddPassengers(C1,C2)
C.PassengerRate()
C.Show_Info()

D.AddAdjacent_bus_station()
D.AddPassengers(D1,D2,D3)
D.PassengerRate()
D.Show_Info()


There are errors in

 for PG in self.passenger:

            info = "the name of passenger is "+ PG.name +"the start-station of passenger is "+PG.start_station+"the destination of passenger is "+ PG.destination_station
            print(info)

The information is:

Traceback (most recent call last):
  File "/home/surface/PycharmProjects/Final Year Project/Bus_Station.py", line 63, in <module>
    A.Show_Info()
  File "/home/surface/PycharmProjects/Final Year Project/Bus_Station.py", line 42, in Show_Info
    info = "the name of passenger is "+ PG.name +"the start-station of passenger is "+PG.start_station+"the destination of passenger is "+ PG.destination_station
AttributeError: 'tuple' object has no attribute 'name'

However, I never create the 'tuple' data structure in the class, and I don't know how to solve it?

2 Answers2

3

Replace

def AddPassengers(self,*passengers):
        self.passenger.append(passengers)

with

 def AddPassengers(self,*passengers):
        self.passenger.extend(passengers)

using *passengers results in passengers being tuple, which you append to self.passenger

buran
  • 13,682
  • 10
  • 36
  • 61
2

Replace

def AddPassengers(self,*passengers):
    self.passenger.append(passengers)

with

def AddPassengers(self,*passengers):
    for passenger in passengers:
        self.passenger.append(passenger)

The append() function takes a single item (not a tuple of items) to be appended to the list.

Maximouse
  • 4,170
  • 1
  • 14
  • 28
  • 1
    To clarify, when you specify `*passengers`, the arguments passed in that position will be placed into a tuple of all the parameters. Your code as it stands appends this tuple as-is to the list of passengers, and the tuple as a whole has no `name` attribute; rather each element in it does – clubby789 Jan 15 '20 at 11:20
  • Thanks! I solve the problem. I just confused how to use asterrisk(*). Now I understand how to use it. – M_L_Sing_Jump_Rap Jan 16 '20 at 04:04