在Python中在控制台打印来自多个数组的值存在问题。

huangapple go评论82阅读模式
英文:

Issues with printing values from multiple arrays in python in the console

问题

这是您要翻译的代码部分:

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()

注意:我已经将代码中的HTML转义字符(如&quot;)更正为双引号(")以使代码更容易阅读。

英文:

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(&quot;Airline | Flight Number | Destination | Gate | Status&quot;)
x = 0
while x &lt; len(a.airline):
print(a.airline + [&quot; | &quot;] + a.flightNumbers + [&quot; | &quot;] + a.destination + [&quot; | &quot;] +  a.gate + [&quot; | &quot;]+  a.status + [&quot;\n&quot;])
x += 1

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

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

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 = &quot;&quot;
def addFlight(self):
while input != &quot;bye&quot;:
# get inputs
air = input(&quot;Enter an airline name &gt;&gt; &quot;)
fn = input(&quot;Enter a flight number &gt;&gt; &quot;)
dest = input(&quot;Enter a destination &gt;&gt; &quot;)
g = input(&quot;Enter a gate number &gt;&gt; &quot;)
stat = input(&quot;Enter a flight status &gt;&gt; &quot;)
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(&quot;Data stored sucessfully.\n&quot;)
a.showAll()
def showAll(self):
print(&quot;Airline | Flight Number | Destination | Gate | Status&quot;)
x = 0
while x &lt; len(a.airline):
print(a.airline + [&quot; | &quot;] + a.flightNumbers + [&quot; | &quot;] + a.destination + [&quot; | &quot;] +  a.gate + [&quot; | &quot;]+  a.status + [&quot;\n&quot;])
x += 1
go = input(&quot;To add a new entry, enter 1.\nTo reprint list, enter 2.\nTo exit, enter 3.\n&quot;)
if go == &quot;1&quot;:
a.addFlight()
elif go == &quot;2&quot;:
for x in range(1,26):
print(&quot; &quot;)
a.showAll()
elif go == &quot;3&quot;:
exit()
if __name__ == &quot;__main__&quot;:
a = FSB()
a.addFlight()

答案1

得分: 1

你正在尝试将字符串"|"连接到您的列表中。请尝试使用["|"]而不是。

英文:

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

答案2

得分: 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!

英文:

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

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

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

huangapple
  • 本文由 发表于 2020年1月4日 01:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定