I'm currently still learning Python, and more specifically OOP, and my teacher has tasked my class with creating a console rendition of patience (or solitaire). I'm fairly confident with objects and methods etc. however there is one specific point I am having trouble with.
To display each tableau in the game, I am trying to use a loop which will spit out each card in individual rows:
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'))
Where tableaus is a list of 7 sublists that can contain separate card objects, depending on the movement of the cards by the player. I have already attempted to catch any exceptions using the try/except clauses shown, however I have realised that no matter how many exceptions I attempt to catch, there are simply too many possibilities in the variety of cards in each tableau. Therefore, as the first tableau always starts with a single card, the first line is displayed as
S JS X X X X X X HF
but as soon as the for loop tries to iterate through the next cards it catches an 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
Does anyone have any alternative suggestion on how I could implement this code to either ignore or replace unindexed values with whitespace, or an alternative approach altogether?
Thanks a lot for even reading this far!
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')
You can catch specific errors with try
and except <ErrorType>
. But when you can catch those errors, you want something that is almost certainly not going to end up with another error, the way you have written your code thus far, the code in your except statement could very easily produce the same error you originally were trying to avoid. I would write it a:
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)
Running the above, will not give any IndexErrors
, and will give you:
$ python3 t.py
S 0000000 HF
S 111111 HF
S 22222 HF
S 3333 HF
S 444 HF
S 55 HF
S 6 HF