2014-04-21 24 views
0

我已经在Python中构建了一个用于在剧院中预订座位的代码。代码工作良好,除了需要能够在循环中重复代码,所以一旦预订了一些座位,您就可以选择预订更多座位。在Python中重复代码

这是代码:

NumSeat = input ("Please enter the number of seats you desire: ") 
print (" ") 
import re 

if re.match("[0-9]", NumSeat): 
    if int(NumSeat) > 6: 
     print ("You may only book a maximum of 6 seats") 
    else: 
     if int(NumSeat) < 1: 
      print ("You must book at least 1 seat") 
     else: 
      SeatRow = input ("Please enter the row you want to sit in: ") 

      if len(SeatRow) > 1: 
       print ("Invalid row") 
      else: 
       if SeatRow.count ("A") or SeatRow.count ("a") == 1: 
        print ("The seats avaiable in row", SeatRow, "for", int(NumSeat), "people are", RowA[0],"-", RowA[int(NumSeat)-1]) 
        RowA = RowA[int(NumSeat):] 
       else: 
        if SeatRow.count ("B") or SeatRow.count ("b") == 1: 
         print ("The seats avaiable in row", SeatRow, "for", int(NumSeat), "people are", RowB[0],"-", RowB[int(NumSeat)-1]) 
         RowB = RowB[int(NumSeat):] 
        else: 
         if SeatRow.count ("C") or SeatRow.count ("c") == 1: 
          print ("The seats avaiable in row", SeatRow, "for", int(NumSeat), "people are", RowC[0],"-", RowC[int(NumSeat)-1]) 
          RowC = RowC[int(NumSeat):] 
         else: 
          if SeatRow.count ("D") or SeatRow.count ("d") == 1: 
           print ("The seats avaiable in row", SeatRow, "for", int(NumSeat), "people are", RowD[0],"-", RowD[int(NumSeat)-1]) 
           RowD = RowD[int(NumSeat):] 
          else: 
           if SeatRow.count ("E") or SeatRow.count ("e") == 1: 
            print ("The seats avaiable in row", SeatRow, "for", int(NumSeat), "people are", RowE[0],"-", RowE[int(NumSeat)-1]) 
            RowE = RowE[int(NumSeat):] 
           else: 
            print("Invalid row") 
else: 
    print ("You must input a number") 

这将是巨大的,如果你有任何建议

+1

哇,这是我见过的最丑陋的代码片段之一。你应该真正考虑软件设计的不要重复自己(DRY)原理。我很抱歉给你这样一个负面的反馈,你当然可能不同意,但帮助你纠缠在更多的重复中...会对自己产生偏见。你应该寻求重新设计这个帮助,而不是为了帮助它以某种方式工作。 – logc

+0

我之前做过工作,但在发布之后我收拾整理了 – user3556246

回答

0

但你的问题是,为了避免重复,你可以创建一个字典。你可以通过使用一个字典,其中的行是关键字,并添加里面的座位数组,删除if/else或elif。例如,您可能需要强制该行成为第一个元素a1,如果不需要某些解析。例如,你可以这样做:

import re 

#initialize the rows, 
valid_rows = [ 'a','b','c','d'] 
row_size = 10 # 10 seats per row 

rows = dict(zip(valid_rows, [range(row_size)]*len(valid_rows))) # as suggested by Burhan in comments 

# a forever loop booking seats (if no errors happen) 
while(True): 
    num_seat = int(raw_input ("Please enter the number of seats you desire: ")) 
    if re.match("[0-9]", str(num_seat)): 
     if num_seat > 6: 
      print ("You may only book a maximum of 6 seats") 
     elif num_seat < 1: 
      print ("You must book at least 1 seat") 
     else: 
      seat_row = raw_input("Please enter the row you want to sit in: ") 

      if seat_row in valid_rows: 
       if len(rows[seat_row]) < num_seat: 
        print "Not space in that row" 
       else: 
        print ("The seats avaiable in row", seat_row, "for", 
         num_seat, "people are", rows[seat_row][0],"-", rows[seat_row][num_seat-1]) 
        rows[seat_row] = rows[seat_row][num_seat:] 
      else: 
       print("Invalid row") 
+0

嗨,感谢您的回复,但我在寻找的是如何整合“for”或“while”(或其他)声明,将允许我重复我的代码。 – user3556246

+0

你不需要为或检查列/行 –

+0

'rows = dict(zip(valid_rows,[row_size] * len(valid_rows)))' –