gpt4 book ai didi

python - 使用循环更改二维列表中的多个值

转载 作者:行者123 更新时间:2023-12-01 09:14:31 27 4
gpt4 key购买 nike

我正在编写的程序是在将座位创建为 5 行 5 列二维列表后“出售”剧院中的座位。我已经让它在大部分情况下都能工作,但如果我选择一个已标记为已售出的座位旁边的座位,我会收到一条消息,就好像该座位也已售出一样。这是我正在使用的标准:

Fill a 5 x 5 grid. Then do the following. Create a loop that will continue until the user says (Y/N) they want to quit, or all 25 seats are sold. Have the users ask for a seat position based on row and column (row, column) - remember that the list’s positions start at 0, but the user will ask for the seat starting at row or column position 1. Then when the user selects the seat, print out the user's price and mark an 'SS' (sold seat) in the List where the price was. Then print out all the seats so the user can select another. When the next user in the loop, asks for a seat make sure you check and see if that seat is already sold (has an ‘SS’ in the table). If it is sold, tell the user – otherwise return the price and mark the seat sold. After the user asks to quit or all the seats are sold, or the loop ends, then printout the list as shown below - showing the sold seats (SS) and unsold seats (the unsold seat should still have a price).

这是我的代码:

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)

关于python - 使用循环更改二维列表中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51372775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com