def display(taken_seats):
seating = []
xd = 0
while xd < 15: #creates the cinema seating map
row = []
xda = 0
while xda < 15:
row.append("#")
xda = xda + 1
xd = xd + 1
seating.append(row)
for x in taken_seats: #changes the seats that are taken to a "X"
pos = x.split(",")
seating[(int(pos[0]) - 1)][(int(pos[1]))] = "X"
dx = 1
for row in seating: #prints the rows.
if len(str(dx)) < 2:
de = " " + str(dx)
else:
de = dx
print "Row: " + str(de) + " ".join(row)
dx = dx + 1
taken_seats = ("1,12", "2,8", "5,5", "12,12")
display(taken_seats)
所有购买的座位都被放在“座位”的列表中,格式为“座位”
希望这有帮助!(我很抱歉它不是最干净的,它主要是迭代器。)
编辑:
要增加额外的座位,只需
^{pr2}$
将号码更改到所需的座位位置。在
要显示更新的座位,只需运行命令display(taken_seats)
带有菜单界面的代码:def display_seats(taken_seats):
seating = []
for xd in range(15):
row = []
xda = 0
for xda in range(15):
row.append("#")
seating.append(row)
for x in taken_seats:
pos = x.split(",")
seating[(int(pos[0]) - 1)][(int(pos[1]))] = "X"
dx = 1
for row in seating:
if len(str(dx)) < 2:
de = " " + str(dx)
else:
de = dx
print ("Row: " + str(de) + " ".join(row))
dx = dx + 1
def list_options():
print ("5: exit")
print ("4: buy ticket")
print ("3: view sales")
print ("2: view price per row")
print ("1: view current seating")
new_input = input("Your choice: ")
return (new_input)
def purchase_seat(taken_seats):
print ("Would you like to view current seating availability? ")
print ("'1' = yes, '2' = no")
newinput = input("? ")
if newinput == "1":
display_seats(taken_seats)
x = True
while x == True:
cost = 0
print ("what row would you like to buy a seat on? ")
rowx = input("What row? ")
print ("What seat would you like to purchase?")
rowy = input("what seat? ")
if any((str(rowx) + "," + str(rowy)) for x in taken_seats):
print ("That seat is already taken, please choose another seat.")
elif int(rowx) > 15 or int(rowy) > 15:
print ("Invalid seating location, please choose another seat.")
else:
print ("seat purchased.")
cost = (200 - (10 * int(rowx)))
x = False
return (cost, (str(rowx) + "," + str(rowy)))
taken_seats = []
sales = 0
quitter = 0
while quitter == 0:
new_input = list_options()
if new_input == "5":
quitter = 1
elif new_input == "4":
g = True
while g == True:
new_seat = purchase_seat(taken_seats)
taken_seats.append(new_seat[1])
print ("That will be: $ " + str(new_seat[0]))
sales = sales + new_seat[0]
print ("Would you like to purchase another seat?")
new_input = input("'1' = yes, '2' = no: ")
if new_input == "1":
pass
else:
g = False
elif new_input == "3":
print ("Total sales: $" + str(sales))
elif new_input == "2":
xd = 0
while xd < 15:
print ("Row " + str(xd + 1) + ": is $" + str((200 - (10 * xd)) - 10))
xd = xd + 1
elif new_input == "1":
display_seats(taken_seats)
else:
print ("invalid option.")
在gaelephor.com中可以找到一个zip文件,其中包含要从文件中保存和加载的座位和销售的文件和代码