2016-12-11 78 views
-5

我試圖編寫類似這樣的代碼。這就像電影院的座位計劃。Python中的座位平面圖

Row 1 ############### 

Row 2 ############### 

Row 3 ############### 

直到第15行,每行有由代表15位:#

我這樣做是使用許多「打印」,但時間太長。

每個#的位置必須是一個列表。如果有人選擇一個座位,例如第二排的第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種選擇:退出程序。

請注意,如果我們不選擇第5個選項,程序不應該結束。

+3

聽起來像作業,向我們展示您嘗試的內容。 – saarrrr

+0

是的,我可以。它實際上是在法國,但我會翻譯它。只要給我一分鐘來編輯我的問題。 – Alpha

回答

0
def save(sales, taken_seats): 
    #code here to save sales and taken seats to the file. 
    f = open("data.txt", "w") 
    f.write(str(sales) + "\n") 
    for x in taken_seats: 
     f.write(x + "\n") 
    f.close() 
def load(): 
    #code here to load the sales total and currently purchased seats. 
    f = open("data.txt") 
    sales = f.readline().replace("\n", "") 
    taken_seats = [] 
    d = True 
    while d == True: 
     line = f.readline().replace("\n", "") 
     if line == "": 
      d = False 
     else: 
      taken_seats.append(line) 
    if sales == "": 
     sales = 0 
    else: 
     sales = int(sales) 
    f.close() 
    return (int(sales), 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? ") 
     d = (str(rowx) + "," + str(rowy)) 
     h = 0 
     for x in taken_seats: 
      if d == x: 
       h = 1 
     if h == 1: 
      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))) 
da = load() 
sales = da[0] 
taken_seats = da[1] 
quitter = 0 
while quitter == 0: 
    new_input = list_options() 
    if new_input == "5": 
     save(sales, taken_seats) 
     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文件可以在以下位置找到: Download seating.zip

+0

「。它說無效的選擇?我如何選擇一個選項? – Alpha

+0

@Alpha是你在python 2.7還是3? – ANVGJSENGRDG

+0

我使用的是帶有Anaconda的spyder。我認爲這是3 – Alpha

-1
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") 

改變號碼所需的座椅位置。

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文件:

與菜單界面顯示更新的座位,只需運行命令

display(taken_seats) 

代碼並從文件中加載可以在gaelephor.com

+0

嘿謝謝,我會看看它。這可能需要一些時間,因爲我是編程新手。 – Alpha

+0

我該如何選擇特定的座位?根據你的代碼,沒有選擇。 – Alpha

+0

@Alpha當我選擇像1或2這樣的數字時,您會使用命令「taken_seats.append(」10,10「)」 – ANVGJSENGRDG