Python 使用循环更改二维列表中的多个值
我正在写的程序是在将座位创建为5行5列的二维列表后,在剧院“出售”座位。我已经让它在大多数情况下工作,但如果我选择一个座位旁边的一个已经被标记为出售,我得到的信息,如果这个座位也被出售。 以下是我正在使用的标准: 填充一个5 x 5的网格。然后执行以下操作。创建一个将 继续,直到用户说(是/否)他们想退出,或全部25个席位 都卖了。让用户根据世界其他地区和地区要求座椅位置 列(行,列)-记住列表的位置从0开始, 但用户将要求从行或列位置开始的座位 1.然后,当用户选择座位时,打印出用户的价格,并在价格所在的列表中标记“SS”(已售出座位)。然后 打印出所有座位,以便用户可以选择其他座位。下次什么时候 循环中的用户要求一个座位,确保您检查并查看是否有 座位已售出(表中有一个“SS”)。如果售出,请告知 用户–否则返回价格并标记座位已售出。之后 用户要求退出或所有座位售出,或循环结束, 然后打印出如下所示的列表-显示售出的座位(SS) 和未售出的座位(未售出的座位仍有价格) 这是我的代码:Python 使用循环更改二维列表中的多个值,python,python-3.x,list,multidimensional-array,Python,Python 3.x,List,Multidimensional Array,我正在写的程序是在将座位创建为5行5列的二维列表后,在剧院“出售”座位。我已经让它在大多数情况下工作,但如果我选择一个座位旁边的一个已经被标记为出售,我得到的信息,如果这个座位也被出售。 以下是我正在使用的标准: 填充一个5 x 5的网格。然后执行以下操作。创建一个将 继续,直到用户说(是/否)他们想退出,或全部25个席位 都卖了。让用户根据世界其他地区和地区要求座椅位置 列(行,列)-记住列表的位置从0开始, 但用户将要求从行或列位置开始的座位 1.然后,当用户选择座位时,打印出用户的价格,并
def main():
seatList = [
[50,50,50,50,50],
[40,45,45,45,40],
[30,35,35,35,30],
[20,20,20,20,20],
[10,10,10,10,10],
]
cont = "y"
while cont.lower() == "y":
print("Here is the seating arrangement:")
availableSeat = seatPrinter(seatList)
totalRow = int(len(seatList)) - 1
totalColumn = int(len(seatList[0])) - 1
seatRow = int(input("Please enter a row number (1 to %d):"%totalRow))
seatColumn = int(input("Please enter a seat number (1 to %d):"%totalColumn))
seatStatus = seatAvailable(seatList, seatRow, seatColumn)
cont = input("Would you like to reserve another seat?(Y/N)")
def seatPrinter(seats):
for i in range(len(seats[0])):
print(seats[i])
def seatAvailable(seats, row, column):
for i in range(len(seats)):
for j in range(len(seats)):
if seats[i][j] is not 'SS':
seats[i][j] = 'SS'
print("Your seat is in row %d seat number %d"%(i+1, j+1))
return
else:
print("Sorry, that seat isn't available.")
return
main()
现在,这是我得到的输出:
Here is the seating arrangement:
[50, 50, 50, 50, 50]
[40, 45, 45, 45, 40]
[30, 35, 35, 35, 30]
[20, 20, 20, 20, 20]
[10, 10, 10, 10, 10]
Please enter a row number (1 to 4):1
Please enter a seat number (1 to 4):1
Your seat is in row 1 seat number 1
Would you like to reserve another seat?(Y/N)y
Here is the seating arrangement:
['SS', 50, 50, 50, 50]
[40, 45, 45, 45, 40]
[30, 35, 35, 35, 30]
[20, 20, 20, 20, 20]
[10, 10, 10, 10, 10]
Please enter a row number (1 to 4):1
Please enter a seat number (1 to 4):2
Sorry, that seat isn't available.
Would you like to reserve another seat?(Y/N)
我似乎不明白为什么我不能在同一排预定更多的座位,谢谢你的帮助
def main():
seatList = [
[50,50,50,50,50],
[40,45,45,45,40],
[30,35,35,35,30],
[20,20,20,20,20],
[10,10,10,10,10],
]
cont = "y"
while cont.lower() == "y":
print("Here is the seating arrangement:")
availableSeat = seatPrinter(seatList)
totalRow = int(len(seatList)) - 1
totalColumn = int(len(seatList[0])) - 1
seatRow = int(input("Please enter a row number (1 to %d):"%totalRow))
seatColumn = int(input("Please enter a seat number (1 to %d):"%totalColumn))
seatStatus = seatAvailable(seatList, seatRow, seatColumn)
cont = input("Would you like to reserve another seat?(Y/N)")
def seatPrinter(seats):
for i in range(len(seats[0])):
print(seats[i])
def seatAvailable(seats, row, column):
if seats[row-1][column-1] is not 'SS':
seats[row-1][column-1] = 'SS'
print("Your seat is in row %d seat number %d"%(row,column))
return
else:
print("Sorry, that seat isn't available.")
return
main()
结果,
Here is the seating arrangement:
[50, 50, 50, 50, 50]
[40, 45, 45, 45, 40]
[30, 35, 35, 35, 30]
[20, 20, 20, 20, 20]
[10, 10, 10, 10, 10]
Please enter a row number (1 to 4):1
Please enter a seat number (1 to 4):1
Your seat is in row 1 seat number 1
Would you like to reserve another seat?(Y/N)Y
Here is the seating arrangement:
['SS', 50, 50, 50, 50]
[40, 45, 45, 45, 40]
[30, 35, 35, 35, 30]
[20, 20, 20, 20, 20]
[10, 10, 10, 10, 10]
Please enter a row number (1 to 4):1
Please enter a seat number (1 to 4):2
Your seat is in row 1 seat number 2
Would you like to reserve another seat?(Y/N)Y
Here is the seating arrangement:
['SS', 'SS', 50, 50, 50]
[40, 45, 45, 45, 40]
[30, 35, 35, 35, 30]
[20, 20, 20, 20, 20]
[10, 10, 10, 10, 10]
Please enter a row number (1 to 4):1
Please enter a seat number (1 to 4):1
Sorry, that seat isn't available.
Would you like to reserve another seat?(Y/N)
您不必使用for循环来检查座位的可用性,只需使用索引行和列/。啊,这太好了!谢谢在重读这篇文章之后,我也意识到,如果所有座位都卖完了,我还没有在部分中添加关于停车的内容。