gpt4 book ai didi

python - 有没有办法在打印过程中忽略 IndexErrors 并用空格替换?

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:06 24 4
gpt4 key购买 nike

我目前仍在学习 Python,更具体地说是 OOP,我的老师给我的类(class)分配了创建耐心(或纸牌)控制台表演的任务。我对对象和方法等相当有信心。但是有一点我遇到了麻烦。

为了显示游戏中的每个画面,我尝试使用一个循环,它将在单独的行中吐出每张卡:

noColumns = 0
for tableau in self.tableaus:
if len(tableau) > noColumns:
noColumns = len(tableau)

for column in range(noColumns):
if column == 0:
print('S ', self.tableaus[0][column].showCard(), self.tableaus[1][column].showCard(), self.tableaus[2][column].showCard(), self.tableaus[3][column].showCard(),
self.tableaus[4][column].showCard(), self.tableaus[5][column].showCard(), self.tableaus[6][column].showCard(), ' HF')
elif column > 0 and column < 4:
try:
print(self.waste[column-1].showCard(), ' ', self.tableaus[0][column].showCard(), self.tableaus[1][column].showCard(), self.tableaus[2][column].showCard(), self.tableaus[3][column].showCard(),
self.tableaus[4][column].showCard(), self.tableaus[5][column].showCard(), self.tableaus[6][column].showCard(), (self.gameAttributes['Suits'][column]+'F'))
except:
print(' ', self.tableaus[0][column].showCard(), self.tableaus[1][column].showCard(), self.tableaus[2][column].showCard(), self.tableaus[3][column].showCard(),
self.tableaus[4][column].showCard(), self.tableaus[5][column].showCard(), self.tableaus[6][column].showCard(), (self.gameAttributes['Suits'][column]+'F'))

其中tableaus是一个由7个子列表组成的列表,这些子列表可以包含单独的卡片对象,具体取决于玩家对卡片的移动。我已经尝试使用所示的 try/except 子句来捕获任何异常,但是我意识到,无论我 try catch 多少个异常,每个画面中的各种卡片都有太多的可能性。因此,由于第一个画面始终以单张卡片开始,因此第一行显示为

S JS X X X X X X HF

但是一旦 for 循环尝试迭代下一张卡片,它就会捕获 IndexError:

    Traceback (most recent call last):
File "C:\Users\xxxxxx\Desktop\solitaire or patience.py", line 67, in gameDisplay
print(self.waste[column-1].showCard(), ' ', self.tableaus[0][column].showCard(), self.tableaus[1][column].showCard(), self.tableaus[2][column].showCard(), self.tableaus[3][column].showCard(),
IndexError: list index out of range

对于如何实现此代码以忽略或用空格替换未索引的值,或者完全使用替代方法,有人有任何其他建议吗?

非常感谢您阅读本文!

编辑 - 最小示例:

tableaus = [[0],[0,1],[0,1,2],[0,1,2,3],[0,1,2,3,4],[0,1,2,3,4,5],[0,1,2,3,4,5,6]]

longest = 0
for tableau in tableaus:
if len(tableau)>longest:
longest = len(tableau)

for column in range(longest):
print('S ', tableaus[0][column], tableaus[1][column], tableaus[2][column], tableaus[3][column],
tableaus[4][column], tableaus[5][column], tableaus[6][column], ' HF')

最佳答案

您可以使用 try 捕获特定错误和except <ErrorType> 。但是,当您可以捕获这些错误时,您想要的东西几乎肯定不会以另一个错误结束,按照您迄今为止编写代码的方式,您的 except 语句中的代码很容易产生与您最初相同的错误试图避免。我会把它写成:

tableaus = [[0],[0,1],[0,1,2],[0,1,2,3],[0,1,2,3,4],[0,1,2,3,4,5], 
[0,1,2,3,4,5,6]]

longest = 0
for tableau in tableaus:
if len(tableau)>longest:
longest = len(tableau)

for column in range(longest):
string_start = "S "
string_middle = ""
for i in range(7):
try:
x = str(tableaus[i][column])
except IndexError:
x = " "
string_middle += x
string_end = " HF"

final_string = string_start + string_middle + string_end
print(final_string)

运行上面的代码,不会给出任何IndexErrors ,并且会给你:

$ python3 t.py
S 0000000 HF
S 111111 HF
S 22222 HF
S 3333 HF
S 444 HF
S 55 HF
S 6 HF

关于python - 有没有办法在打印过程中忽略 IndexErrors 并用空格替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537984/

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