在Python中重复代码

2024-06-26 13:42:09 发布

您现在位置: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")

如果你有什么建议就太好了


Tags: ortheinforifcountpeopleelse
1条回答
网友
1楼 · 发布于 2024-06-26 13:42:09

但你的问题是为了避免重复,你可以创建一本字典。通过使用以row为键的字典,并在其中添加座位数组,可以完全删除if/else或if-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")

相关问题 更多 >