标题 我目前正试图让用户使用破折号 (-) 和与棋子对应的字母来输入棋盘。但是列表没有正确保存。这是搞砸的代码。
def make_a_chessboard():
chessboard = []
possible_char = ["-","K","k","Q","q","R","r","N","n","B","b","P","p"]
rows = 8
cols = 8
for r in range(rows):
user_input = input("")
while len(user_input) != 8:
print("That is not the correct length. Please try again.")
user_input = input("")
for i in range(len(user_input)):
flag1 = False
while flag1 == False:
if user_input[i] not in possible_char:
print("One of the characters used is not supported. Please try again.")
user_input = input("")
else:
for c in range(cols):
chessboard[r][c].append(user_input[c])
flag1 = True
return(chessboard)
这给了我 IndexError: list index out of range 错误。我究竟做错了什么?