0
我想創建一個多維列表,其中程序中用戶要求輸入座位號或價格,然後用0
替換它們。多維列表Python(初學者)
這是我到目前爲止有:
board = [10,10,10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,10,10,
10,10,20,20,20,20,20,20,10,10,
10,10,20,20,20,20,20,20,10,10,
10,10,20,20,20,20,20,20,10,10,
20,20,30,30,40,40,30,30,20,20,
20,30,30,40,50,50,40,30,30,20,
30,40,50,50,50,50,50,50,40,30,]
def show():
print(board [0], board[1], board[2], board[3], board[4], board[5], board[6], board[7], board[8], board[9])
print(board [10], board[11], board[12], board[13], board[14], board[15], board[16], board[17], board[18], board[19])
print(board [20], board[21], board[22], board[23], board[24], board[25], board[26], board[27], board[28], board[29])
print(board [30], board[31], board[32], board[33], board[34], board[35], board[36], board[37], board[38], board[39])
print(board [40], board[41], board[42], board[43], board[44], board[45], board[46], board[47], board[48], board[49])
print(board [50], board[51], board[52], board[53], board[54], board[55], board[56], board[57], board[58], board[59])
print(board [60], board[61], board[62], board[63], board[64], board[65], board[66], board[67], board[68], board[69])
print(board [70], board[71], board[72], board[73], board[74], board[75], board[76], board[77], board[78], board[79])
print(board [80], board[81], board[82], board[83], board[84], board[85], board[86], board[87], board[88], board[89])
while True:
seat = int(input("Please enter a seat or price"))
if board[seat] != "0" :
board[seat] = "0"
else:
print("This seat is laready sold")
show()
的問題是,我無法弄清楚如何更換零的索引值時,給出例如價格如果用戶輸入的是價格「40」而不是替換索引值40我想替換價格數字「40」。
我希望這是有道理的。
順便說一句,這與你的問題沒有關係,但你可能會考慮使用類似print(board [0:10])的東西,並可能將它用於for循環。這樣你就可以抽象你的代碼,也可以節省一些時間! – qwertyuip9
正如@ qwertyuip9所說,在range(0,90,10):print(* board [i:i + 10])中使用'for i'。 – TigerhawkT3
此外,您的代碼的'board'包含整數,但您將它們與該循環中的字符串進行比較。 – TigerhawkT3