0

So I am trying to make a program that take in input for a flight, and stores it in arrays based on each type of input. Here are the arrays that I use to do this:

airline = []
flightNumbers = []
destination = []
gate = []
status = []

Here is the issue that I am having. After the user goes through and adds 1 flight, I want the program to print a flight status board in the console. For example if I enter: "Delta", "Dl1480", "Atlanta", "E11", "Ontime" "American", "AA367", "New York City", "H10", "Delayed" "United", "UA3411", "Louisville, KY", "F25", "Cancelled" this is what I want to see output by the program:

airline:  | flight number:  | destination:     |  gate:  |  status:
--------------------------------------------------------------------
Delta     |  DL1480         |  Atlanta         |  E11    | Ontime
American  |  AA367          |  New York City   |  H10    | Delayed
United    |  UA3417         |  Louisville,KY   |  F25    | Cancelled

Here is what I tried to use to get this to print:

def showAll(self):
            print("Airline | Flight Number | Destination | Gate | Status")
            x = 0
            while x < len(a.airline):
                print(a.airline + [" | "] + a.flightNumbers + [" | "] + a.destination + [" | "] +  a.gate + [" | "]+  a.status + ["\n"])
                x += 1

but I get this as output if I enter 2 random entries:

Airline | Flight Number | Destination | Gate | Status
['delta', 'delta', ' | ', '001', '002', ' | ', 'Los angeles, ca', 'atlanta', ' | ', 'a1', 'a3', ' | ', 'ontime', 'ontime', '\n']
['delta', 'delta', ' | ', '001', '002', ' | ', 'Los angeles, ca', 'atlanta', ' | ', 'a1', 'a3', ' | ', 'ontime', 'ontime', '\n']

Can some suggest a way I can fix this, or a better way of going about this entirely? Here is the code for the entire program:

class FSB:
        # arrays to store flight information
        airline = []
        flightNumbers = []
        destination = []
        gate = []
        status = []
        input = ""

        def addFlight(self):

            while input != "bye":
                # get inputs
                air = input("Enter an airline name >> ")
                fn = input("Enter a flight number >> ")
                dest = input("Enter a destination >> ")
                g = input("Enter a gate number >> ")
                stat = input("Enter a flight status >> ")
                self.air = air
                self.fn = fn
                self.dest = dest
                self.g = g
                self.stat = stat


                # add inputs to appropiate arrays
                a.airline.append(self.air)
                a.flightNumbers.append(self.fn)
                a.destination.append(self.dest)
                a.gate.append(self.g)
                a.status.append(self.stat)
                print("Data stored sucessfully.\n")
                a.showAll()

        def showAll(self):
            print("Airline | Flight Number | Destination | Gate | Status")
            x = 0
            while x < len(a.airline):
                print(a.airline + [" | "] + a.flightNumbers + [" | "] + a.destination + [" | "] +  a.gate + [" | "]+  a.status + ["\n"])
                x += 1

            go = input("To add a new entry, enter 1.\nTo reprint list, enter 2.\nTo exit, enter 3.\n")
            if go == "1":
                a.addFlight()
            elif go == "2":
                for x in range(1,26):
                    print(" ")
                a.showAll()
            elif go == "3":
                exit()

if __name__ == "__main__":
    a = FSB()
    a.addFlight()
1
  • You should use self instead of a inside the methods. You should not read inputs inside of the methods. Commented Jan 3, 2020 at 17:15

2 Answers 2

1

You're trying to concatenate a string "|" to your list. Please try doing ["|"]instead.

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

2 Comments

So that fixed my original issue, but now it prints that same entry 5 times. What kind of loop should I use to make sure that it prints one flight entry once only?
Try doing a.airline[x] ? Alternatively try using a while loop where you manually increment.
1

I ended up iterating through each array manually, and this is what I got:

  def showAll(self):
            print("Airline\t\t\t" +"| Flight Number\t\t" +"| Destination\t\t" +"| Gate \t\t" +"| Status")
            for x in range(len(a.airline)):
                print("-------------------------------------------------------------------------------------------------------------------")
                print(str(a.airline[x] + "\t\t") + str(a.flightNumbers[x] + "\t\t") + str(a.destination[x] + "\t\t\t\t") + str(a.gate[x] + "\t\t")  + str(a.status[x]))

Thank you to everyone who suggested an answer, I appreciate it!

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.