I'm trying to create some sort of card game in python but I can't get past a pretty crucial part of the design-designing the card faces as callable objects. I have my array of just heart cards below (I tried to divide the card suits into 4 different functions because I can't figure out how to make it one function) but when I try to print the art it comes out all in one line.
heartsArt = {
1 : (
'┌───┐',
'| 1 |',
'└───┘',
),
2 : (
'┌───┐',
'| 2 |',
'└───┘',
),
3 : (
'┌───┐',
'| 3 |',
'└───┘',
),
4 : (
'┌───┐',
'| 4 |',
'└───┘',
),
5 : (
'┌───┐',
'| 5 |',
'└───┘',
),
6 : (
'┌───┐',
'| 6 |',
'└───┘',
),
7 : (
'┌───┐',
'| 7 |',
'└───┘',
),
8 : (
'┌───┐',
'| 8 |',
'└───┘',
),
}
cardHeight = len(heartsArt[1])
cardLength = len(heartsArt[1][0])
cardFaceSeparator = ''
def generateHeartFace(cardValues):
cardFaces = []
for x in cardValues:
cardFaces.append(heartsArt[x])
for xID in range(cardHeight):
rowComp = []
for card in cardFaces:
rowComp.append(heartsArt[card]) #line 57
rowString = cardFaceSeparator.join(rowComp)
cardFaceRows.append(rowString)
generateHeartFace([1]) #line 62
The error that comes up is:
Traceback (most recent call last)
File "script.py", line 62 in <module> generateHeartFace([1])
File "script.py", line 57, in generateHeartFace
rowComp.append(heartsArt[card])
KeyError: ('┌───┐', '| 1 |', '└───┘')
If someone could please explain both/either why my code doesn't work, or how I could make it work it would be greatly appreciated. Thanks in advance :)
CodePudding user response:
I think I've answered all your questions in the comments. Here's your code working:
def generateHeartFace(cardValues):
cardFaces = []
for x in cardValues:
cardFaces.append(heartsArt[x])
cardFaceRows = []
for xID in range(cardHeight):
rowComp = []
for card in cardFaces:
rowComp.append(card[xID]) #line 57
rowString = cardFaceSeparator.join(rowComp)
cardFaceRows.append(rowString)
print('\n'.join(cardFaceRows))
Don't forget to pass an iterable:
generateHeartFace([1,2,3])
And here's another way to do it that does not require the heartsArt dictionary:
def generateHeartFace(cardValues):
print(cardFaceSeparator.join("┌───┐" * len(cardValues)))
print(cardFaceSeparator.join(f"| {c} |" for c in cardValues))
print(cardFaceSeparator.join("└───┘" * len(cardValues)))
