Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #Purpose: Write a program that prompts users to pick either a seat or a #price.
  2. #Mark sold seats by changing the price to 0. When a user specifies a seat, #make sure it is available.
  3. #When a user specifies a price, find any seat with that price.
  4.  
  5. import csv
  6. print("Welcome to TickeTron!")
  7.  
  8. # Format the text file
  9. seats = open("tickets.txt", 'r')
  10. seatsRead = open("tickets.txt", 'r').read()
  11.  
  12. # Convert the reader data into a formatted table/2d list (lines 18 to 24)
  13. reader = csv.reader(seats)
  14. seatsList = []
  15. for row in list(reader):
  16. newRow = []
  17. # Add each seat into the row as an int
  18. for seat in row[0].split(" "):
  19. newRow.append(int(seat))
  20. # Add the row to the table
  21. seatsList.append(newRow)
  22.  
  23. # Convert a row index to an ascii row letter (EX: 0 -> A, 1 -> B, ...)
  24. def rowToLetter(row):
  25. return chr(97+row)
  26.  
  27. # Convert a ascii row letter to a row index (EX: A -> 0, B -> 1, ...)
  28. def letterToRow(letter):
  29. return ord(letter.lower())-97
  30.  
  31. # Get the seat price from the table with the highest number of digits
  32. def getLongestSeatLength():
  33. largest = len(str(seatsList[0][0]))
  34. for row in seatsList:
  35. for seat in row:
  36. if len(str(seat)) > largest:
  37. largest = len(str(seat))
  38. return largest
  39.  
  40. # Print a table populated with the formatted text file
  41. def printSeats():
  42. longest = getLongestSeatLength()
  43. numbers = " "
  44. # Calculate the columns header for the table (1 2 3 4 5 6 7 8 9)
  45. for i in range(len(seatsList[0])):
  46. padding = longest-1
  47. numbers += " "*padding + str(i) + " "
  48. print(f"n{numbers}")
  49.  
  50. for row in range(len(seatsList)):
  51. # Print the row header (A B C D E F G H I)
  52. print(chr(65+row) + " ", end="")
  53. # Calculate the position of each seat in the table
  54. for seat in seatsList[row]:
  55. padding = longest-len(str(seat))
  56. print(" "*padding + str(seat), "", end="")
  57. print()
  58. print()
  59.  
  60. print("Here is a listing of all available seats:")
  61.  
  62. printSeats()
  63.  
  64. # Check if a seat is available for purchase (seat price is not zero in seatsList)
  65. def seatAvailable(row, col):
  66. return seatsList[row][col] != 0
  67.  
  68. def purchaseSeat(seat, cost=-1):
  69. row = letterToRow(seat[0])
  70. col = int(seat[1])
  71. # Only run next lines if the seat is available and the price is equal to the required price
  72. if seatAvailable(row, col) and (cost == -1 or seatsList[row][col] == cost):
  73. print("Your seat is now at %s%d!" % (rowToLetter(row).upper(), col))
  74. seatsList[row][col] = 0
  75. printSeats()
  76. return True
  77. # Print an error if the seat is not available
  78. elif not seatAvailable(row, col):
  79. print("That seat is unavailable. Please try again.")
  80. return False
  81. # Print an error if the requested seat does not have the required price
  82. else:
  83. print("Seat %s%d is not worth $%d. Please try again." % (rowToLetter(row).upper(), col, cost))
  84. return False
  85.  
  86. def findSeat(seatsList):
  87. selection1 = " "
  88. # Continue asking the user to purchase a seat until selection1 is not empty
  89. while (selection1 != ""):
  90. selection1 = str(input("Would you like to purchase a specific seat? "))
  91. if selection1.lower() == "y" or selection1.lower() == "yes":
  92. seat = str(input("Please enter the specific seat you wish to purchase: "))
  93. # If the purchase is not successful, ask the user again
  94. if len(seat) > 2:
  95. print("Sorry, it seems you have entered an incorrect input.")
  96. continue
  97. if not purchaseSeat(seat):
  98. selection1 = " "
  99. elif selection1.lower() == "n" or selection1.lower() == "no":
  100. selection2 = str(input("Would you like to purchase a seat for a specific price? "))
  101. if selection2 == "y" or selection2 == "yes":
  102. price = int(input("Please enter the price you would like to pay: ").replace("$", ""))
  103. seat = str(input("Please enter the specific seat you wish to purchase: "))
  104. # If the purchase if not successful, ask the user again
  105. if not purchaseSeat(seat, price):
  106. selection1 = " "
  107. else:
  108. selection1 = ""
  109.  
  110. findSeat(seatsList)
  111. print("Very well, ending the program.")
  112.  
  113. 10 10 10 10 10 10 10 10 10 10
  114. 10 10 10 10 10 10 10 10 10 10
  115. 10 10 10 10 10 10 10 10 10 10
  116. 10 10 20 20 20 20 20 20 10 10
  117. 10 10 20 20 20 20 20 20 10 10
  118. 10 10 20 20 20 20 20 20 10 10
  119. 20 20 30 30 40 40 30 30 20 20
  120. 20 30 30 40 50 50 40 30 30 20
  121. 30 40 50 50 50 50 50 50 40 30
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement