我试图编写类似这样的代码。这就像电影院的座位安排。
Row 1 ###############
Row 2 ###############
Row 3 ###############
直到第15行,每行有15个位置代表:#
我使用了很多" print"但那太长了。
每个#的位置必须是一个列表。如果有人选择座位,例如第2排的第15个座位而不是#应该用*代替。
我只是想知道如何使用While或For和列表而不是打印#15次?
这是我的代码:
def MenuPrincipal():
print("1. Afficher les places disponibles ")
print("2. Afficher les tarifs")
print("3. Afficher le total des ventes")
print("4. Acheter un billet")
print("5. Quitter")
print()
def Place():
Rang1 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang2 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang3 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang4 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang5 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang6 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang7 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang8 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang9 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang10 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang11 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang12 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang13 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang14 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
Rang15 = ('#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#')
print("Rangé\t1\t",Rang1[0],Rang1[1],Rang1[2],Rang1[3],Rang1[4],Rang1[5],Rang1[6],
Rang1[7],Rang1[8],Rang1[9],Rang1[10],Rang1[11],Rang1[12],Rang1[13],Rang1[14],Rang1[15],Rang1[16],Rang1[17],Rang1[18],Rang1[19],Rang1[20],
Rang1[21],Rang1[22],Rang1[23],Rang1[24],Rang1[25],Rang1[26],Rang1[27],Rang1[28],Rang1[29],sep='')
print("Rangé\t2\t",Rang2[0],Rang2[1],Rang2[2],Rang2[3],Rang2[4],Rang2[5],Rang2[6],
Rang2[7],Rang2[8],Rang2[9],Rang2[10],Rang2[11],Rang2[12],Rang2[13],Rang2[14],Rang2[15],Rang2[16],Rang2[17],Rang2[18],Rang2[19],Rang2[20],
Rang2[21],Rang2[22],Rang2[23],Rang2[24],Rang2[25],Rang2[26],Rang2[27],Rang2[28],Rang2[29],sep='')
MenuPrincipal()
Choix1 = input("Veuillez SVP choisir une option (1 – 5):")
if Choix1 == '5':
exit
elif Choix1 == '1':
Place()
我还没有完成,但我会解释发生了什么。
我们有5个选项。
第一选项:显示座位图和座位可用性。
第二个选项:显示每行的价格。例如,第一行= 200 $,第二行= 180 $,等等。
第3个选项:显示总销售额。
第4个选项:提供选择特定座位和购票的选项。
第五个选项:退出程序。
请注意,如果我们不选择第5个选项,程序不应该结束。
答案 0 :(得分:1)
# http://www.fileformat.info/format/mpeg/sample/index.dir
import pygame
from tkinter import *
import os
def play():
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(screen,(0,0))
pygame.display.update()
clock.tick(FPS)
root = Tk()
FPS = 60
pygame.init()
pygame.display.init()
clock = pygame.time.Clock()
embed = Frame(root, width = 500, height = 500) #creates embed frame for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left
buttonwin = Frame(root, width = 75, height = 500)
buttonwin.pack(side = LEFT)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
movie = pygame.movie.Movie('MELT.MPG')
screen = pygame.display.set_mode((500,500))
movie.set_display(screen)
button1 = Button(buttonwin,text = 'Play', command=play)
button1.pack(side=LEFT)
while True:
root.after(0,pygame.display.update)
root.mainloop()
包含带文件保存的脚本的zip文件可在以下位置找到: Download seating.zip
答案 1 :(得分:0)
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)
所有购买的座位都以##,#座位#格式列入“taken_seats”列表 希望这有帮助! (我很抱歉它不是最干净的,它主要是用迭代器填充的。)
编辑:
添加额外席位,只需
taken_seats.append("10,10")
将数字更改为所需的座位位置。
显示更新的座位,只需运行命令
即可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.")
找到包含要从文件保存和加载的座位和销售的文件和代码的zip文件。