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

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

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

问题

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

  1. 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:
  2. airline = []
  3. flightNumbers = []
  4. destination = []
  5. gate = []
  6. status = []
  7. 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:
  8. "Delta", "Dl1480", "Atlanta", "E11", "Ontime"
  9. "American", "AA367", "New York City", "H10", "Delayed"
  10. "United", "UA3411", "Louisville, KY", "F25", "Cancelled"
  11. this is what I want to see output by the program:
  12. airline: | flight number: | destination: | gate: | status:
  13. --------------------------------------------------------------------
  14. Delta | DL1480 | Atlanta | E11 | Ontime
  15. American | AA367 | New York City | H10 | Delayed
  16. United | UA3417 | Louisville,KY | F25 | Cancelled
  17. Here is what I tried to use to get this to print:
  18. def showAll(self):
  19. print("Airline | Flight Number | Destination | Gate | Status")
  20. x = 0
  21. while x < len(a.airline):
  22. print(a.airline + [" | "] + a.flightNumbers + [" | "] + a.destination + [" | "] + a.gate + [" | "]+ a.status + ["\n"])
  23. x += 1
  24. but I get this as output if I enter 2 random entries:
  25. Airline | Flight Number | Destination | Gate | Status
  26. ['delta', 'delta', ' | ', '001', '002', ' | ', 'Los angeles, ca', 'atlanta', ' | ', 'a1', 'a3', ' | ', 'ontime', 'ontime', '\n']
  27. ['delta', 'delta', ' | ', '001', '002', ' | ', 'Los angeles, ca', 'atlanta', ' | ', 'a1', 'a3', ' | ', 'ontime', 'ontime', '\n']
  28. 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:
  29. class FSB:
  30. # arrays to store flight information
  31. airline = []
  32. flightNumbers = []
  33. destination = []
  34. gate = []
  35. status = []
  36. input = ""
  37. def addFlight(self):
  38. while input != "bye":
  39. # get inputs
  40. air = input("Enter an airline name >> ")
  41. fn = input("Enter a flight number >> ")
  42. dest = input("Enter a destination >> ")
  43. g = input("Enter a gate number >> ")
  44. stat = input("Enter a flight status >> ")
  45. self.air = air
  46. self.fn = fn
  47. self.dest = dest
  48. self.g = g
  49. self.stat = stat
  50. # add inputs to appropiate arrays
  51. a.airline.append(self.air)
  52. a.flightNumbers.append(self.fn)
  53. a.destination.append(self.dest)
  54. a.gate.append(self.g)
  55. a.status.append(self.stat)
  56. print("Data stored sucessfully.\n")
  57. a.showAll()
  58. def showAll(self):
  59. print("Airline | Flight Number | Destination | Gate | Status")
  60. x = 0
  61. while x < len(a.airline):
  62. print(a.airline + [" | "] + a.flightNumbers + [" | "] + a.destination + [" | "] + a.gate + [" | "]+ a.status + ["\n"])
  63. x += 1
  64. go = input("To add a new entry, enter 1.\nTo reprint list, enter 2.\nTo exit, enter 3.\n")
  65. if go == "1":
  66. a.addFlight()
  67. elif go == "2":
  68. for x in range(1,26):
  69. print(" ")
  70. a.showAll()
  71. elif go == "3":
  72. exit()
  73. if __name__ == "__main__":
  74. a = FSB()
  75. 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:

  1. airline = []
  2. flightNumbers = []
  3. destination = []
  4. gate = []
  5. 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:

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

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

  1. def showAll(self):
  2. print(&quot;Airline | Flight Number | Destination | Gate | Status&quot;)
  3. x = 0
  4. while x &lt; len(a.airline):
  5. print(a.airline + [&quot; | &quot;] + a.flightNumbers + [&quot; | &quot;] + a.destination + [&quot; | &quot;] + a.gate + [&quot; | &quot;]+ a.status + [&quot;\n&quot;])
  6. x += 1

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

  1. Airline | Flight Number | Destination | Gate | Status
  2. [&#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;]
  3. [&#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:

  1. class FSB:
  2. # arrays to store flight information
  3. airline = []
  4. flightNumbers = []
  5. destination = []
  6. gate = []
  7. status = []
  8. input = &quot;&quot;
  9. def addFlight(self):
  10. while input != &quot;bye&quot;:
  11. # get inputs
  12. air = input(&quot;Enter an airline name &gt;&gt; &quot;)
  13. fn = input(&quot;Enter a flight number &gt;&gt; &quot;)
  14. dest = input(&quot;Enter a destination &gt;&gt; &quot;)
  15. g = input(&quot;Enter a gate number &gt;&gt; &quot;)
  16. stat = input(&quot;Enter a flight status &gt;&gt; &quot;)
  17. self.air = air
  18. self.fn = fn
  19. self.dest = dest
  20. self.g = g
  21. self.stat = stat
  22. # add inputs to appropiate arrays
  23. a.airline.append(self.air)
  24. a.flightNumbers.append(self.fn)
  25. a.destination.append(self.dest)
  26. a.gate.append(self.g)
  27. a.status.append(self.stat)
  28. print(&quot;Data stored sucessfully.\n&quot;)
  29. a.showAll()
  30. def showAll(self):
  31. print(&quot;Airline | Flight Number | Destination | Gate | Status&quot;)
  32. x = 0
  33. while x &lt; len(a.airline):
  34. print(a.airline + [&quot; | &quot;] + a.flightNumbers + [&quot; | &quot;] + a.destination + [&quot; | &quot;] + a.gate + [&quot; | &quot;]+ a.status + [&quot;\n&quot;])
  35. x += 1
  36. go = input(&quot;To add a new entry, enter 1.\nTo reprint list, enter 2.\nTo exit, enter 3.\n&quot;)
  37. if go == &quot;1&quot;:
  38. a.addFlight()
  39. elif go == &quot;2&quot;:
  40. for x in range(1,26):
  41. print(&quot; &quot;)
  42. a.showAll()
  43. elif go == &quot;3&quot;:
  44. exit()
  45. if __name__ == &quot;__main__&quot;:
  46. a = FSB()
  47. 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:

  1. def showAll(self):
  2. print("Airline\t\t\t| Flight Number\t\t| Destination\t\t| Gate \t\t| Status")
  3. for x in range(len(a.airline)):
  4. print("-------------------------------------------------------------------------------------------------------------------")
  5. 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:

  1. def showAll(self):
  2. 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;)
  3. for x in range(len(a.airline)):
  4. print(&quot;-------------------------------------------------------------------------------------------------------------------&quot;)
  5. 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:

确定