- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前仍在学习 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/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在现代 IDE 中,有一个键盘快捷键可以通过键入文件名称来打开文件,而无需将手放在鼠标上。例如: Eclipse:Cmd|Ctrl + Shift + R -> 打开资源 IntelliJ:Cmd|C
有什么东西会等待事件发生(我正在等待的是 WebBrowser.DocumentCompleted),然后执行代码吗?像这样: If (WebBrowser.DocumentCompleted) 不会
我使用 PHP Minify,它很棒。但我的问题是,是否有任何 PHP 插件或其他东西可以自动检测 javascript/css 代码并自动缩小它?谢谢。 最佳答案 Javascript 压缩器? 看
有没有一种语言,类似什么CoffeeScript是JavaScript,编译成windows batch|cmd|command line的语言? 我指的cmd版本是基于NT的,尤其是XP sp3及以
我知道我可以 ,但是,我真的宁愿有一个任务,我可以从任何可以使用所有(或至少大部分)属性的操作系统调用 copy ,但这并没有消除 unix 上的权限。 我想知道是否已经有解决方案,或者我必须自己编
我正在使用 Vuejs(不使用 jQuery)开发一个项目,该项目需要像 jvectormap 这样的 map 但正如我所说,我没有使用 jQuery,那么是否有任何其他库可以在不使用 jQuery
想要进行一个简单的民意调查,甚至不需要基于 cookie,我不在乎投了多少票。有没有类似的插件或者简单的东西? 最佳答案 这是一个有用的教程 - 让我知道它是否适合您 using jQuery to
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
var FileBuff: TBytes; Pattern: TBytes; begin FileBuff := filetobytes(filename); Result := Co
我想要一个 vqmod xml 文件来添加一次上传多个图像的功能。身边有这样的事吗? 编辑:Opencart版本:2.1.0.1 最佳答案 最后我写了一个xml来添加到opencart 2.1.0.1
所以考虑这样的函数: public void setTemperature(double newTemperatureValue, TemperatureUnit unit) 其中Temperatur
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是 ggplot2 的新手,一直在尝试找到一个全面的美学列表。我想我理解它们的目的,但很难知道哪些可以在各种情况下使用(主要是几何图形?)。 Hadley 的网站偶尔会在各个几何图形的页面上列出可用
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
是否有任何 PHP 函数可以将整数转换为十万和千万? 900800 -> 9,00,800 500800 -> 5,00,800 最佳答案 由于您已在问题标签中添加了 Yii,因此您可以按照 Yii
使用 Clojure 一段时间后,我积累了一些关于它的惰性的知识。我知道诸如map之类的常用API是否是惰性的。然而,当我开始使用一个不熟悉的API(例如with-open)时,我仍然感到怀疑。 是否
我的项目需要一个像 AvalonDock 这样的对接系统,但它的最后一次更新似乎是在 2013 年 6 月。是否有更多...积极开发的东西可以代替它? 最佳答案 AvalonDock 实际上相当成熟并
我正在寻找一个可以逆转 clojure 打嗝的函数 所以 turns into [:html] 等等 根据@kotarak的回答,这现在对我有用: (use 'net.cgrand.enliv
我是一名优秀的程序员,十分优秀!