I have written a program that simulates a TickeTron and allows the user to book seating in a virtual audience. The virtual audience is read in from a text file which will be included below. The program does work when run but I feel there are ways to greatly increase the efficiency of the program. Ugly code is a pet peeve of mine!
The program is slightly lengthy and therefore heavily commented so it is easier to follow along. The code is as follows:
#Purpose: Write a program that prompts users to pick either a seat or a #price.
#Mark sold seats by changing the price to 0. When a user specifies a seat, #make sure it is available.
#When a user specifies a price, find any seat with that price.
import csv
print("Welcome to TickeTron!")
# Format the text file
seats = open("tickets.txt", 'r')
seatsRead = open("tickets.txt", 'r').read()
# Convert the reader data into a formatted table/2d list (lines 18 to 24)
reader = csv.reader(seats)
seatsList = []
for row in list(reader):
newRow = []
# Add each seat into the row as an int
for seat in row[0].split(" "):
newRow.append(int(seat))
# Add the row to the table
seatsList.append(newRow)
# Convert a row index to an ascii row letter (EX: 0 -> A, 1 -> B, ...)
def rowToLetter(row):
return chr(97+row)
# Convert a ascii row letter to a row index (EX: A -> 0, B -> 1, ...)
def letterToRow(letter):
return ord(letter.lower())-97
# Get the seat price from the table with the highest number of digits
def getLongestSeatLength():
largest = len(str(seatsList[0][0]))
for row in seatsList:
for seat in row:
if len(str(seat)) > largest:
largest = len(str(seat))
return largest
# Print a table populated with the formatted text file
def printSeats():
longest = getLongestSeatLength()
numbers = " "
# Calculate the columns header for the table (1 2 3 4 5 6 7 8 9)
for i in range(len(seatsList[0])):
padding = longest-1
numbers += " "*padding + str(i) + " "
print(f"\n{numbers}")
for row in range(len(seatsList)):
# Print the row header (A B C D E F G H I)
print(chr(65+row) + " ", end="")
# Calculate the position of each seat in the table
for seat in seatsList[row]:
padding = longest-len(str(seat))
print(" "*padding + str(seat), "", end="")
print()
print()
print("Here is a listing of all available seats:")
printSeats()
# Check if a seat is available for purchase (seat price is not zero in seatsList)
def seatAvailable(row, col):
return seatsList[row][col] != 0
def purchaseSeat(seat, cost=-1):
row = letterToRow(seat[0])
col = int(seat[1])
# Only run next lines if the seat is available and the price is equal to the required price
if seatAvailable(row, col) and (cost == -1 or seatsList[row][col] == cost):
print("Your seat is now at %s%d!" % (rowToLetter(row).upper(), col))
seatsList[row][col] = 0
printSeats()
return True
# Print an error if the seat is not available
elif not seatAvailable(row, col):
print("That seat is unavailable. Please try again.")
return False
# Print an error if the requested seat does not have the required price
else:
print("Seat %s%d is not worth $%d. Please try again." % (rowToLetter(row).upper(), col, cost))
return False
def findSeat(seatsList):
selection1 = " "
# Continue asking the user to purchase a seat until selection1 is not empty
while (selection1 != ""):
selection1 = str(input("Would you like to purchase a specific seat? "))
if selection1.lower() == "y" or selection1.lower() == "yes":
seat = str(input("Please enter the specific seat you wish to purchase: "))
# If the purchase is not successful, ask the user again
if len(seat) > 2:
print("Sorry, it seems you have entered an incorrect input.")
continue
if not purchaseSeat(seat):
selection1 = " "
elif selection1.lower() == "n" or selection1.lower() == "no":
selection2 = str(input("Would you like to purchase a seat for a specific price? "))
if selection2 == "y" or selection2 == "yes":
price = int(input("Please enter the price you would like to pay: ").replace("$", ""))
seat = str(input("Please enter the specific seat you wish to purchase: "))
# If the purchase if not successful, ask the user again
if not purchaseSeat(seat, price):
selection1 = " "
else:
selection1 = ""
findSeat(seatsList)
print("Very well, ending the program.")
The text file (named "tickets.txt"):
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
20 20 30 30 40 40 30 30 20 20
20 30 30 40 50 50 40 30 30 20
30 40 50 50 50 50 50 50 40 30
If you happen to spot a better way to implement any of my methods, please leave a comment letting me know.