3

I am trying to make a card game using Python. I have this text file below that is the Card ID, Card Name, Card Description, and Card Hit Point.

1, Medusa, Feel The Wraith, 98
2, Gigle , See Him See Him, 54
3, Brozi , Pinch an inch, 91

I have this class that is just a card. What I want to do is have a Deck (list) of these Cards (Class Objects). Here is some code that I am trying to do that will read the text file and place them into objects and then into the list.

import sys
import os
class Card:
    def__init__(self, card_id, name, desc, hp):
        self.card_id = card_id
        self.name = name
        self.desc = desc
        self.hp = hp
        self.cards = []

deck = []

fh = open('dets.txt').readlines()
for line in fh:
    row = line.split(',')
    card_id, name, desc, hp = [i.strip() for i in row]
    card = deck.get(card_id, Card(card_id, name, desc, hp))
    deck[card_id] = card
 

Am I on the track?

6
  • Did you get any error? Or is it working fine? Commented Jul 11, 2019 at 4:53
  • 1
    If you just want general feedback then codereview.stackexchange.com is a better fit Commented Jul 11, 2019 at 4:54
  • 2
    @goodknight, are you aware that list indices of a list l start at 0 up to len(l) - 1? Commented Jul 11, 2019 at 4:54
  • I try to print the object and I am not getting the right results. I think I am not printing the whole thing correct? Commented Jul 11, 2019 at 4:54
  • put space in between def and __init__ @goodknight Commented Jul 11, 2019 at 4:59

4 Answers 4

4

If you just want to put those card objects in a list, do this:

deck = []
fh = open('dets.txt').readlines()
for line in fh:
    row = line.split(',')
    card_id, name, desc, hp = [i.strip() for i in row]
    card = Card(card_id, name, desc, hp)
    deck = deck + [ card ] 

Else, by looking at your code, I think you want to store them in a dictionary with card_id as key. To store them in a dictionary,

deck = {} #dictionary
fh = open('dets.txt').readlines()
for line in fh:
    row = line.split(',')
    card_id, name, desc, hp = [i.strip() for i in row]
    card = Card(card_id, name, desc, hp)
    deck[card_id] = card #'card_id' is the 'key' and 'card' is 'value'

To print the name of cards in the dictionary, use for-loop,

for each_key in deck:
    print(each_key, " - ", deck[each_key].name )

This will give output:

1  -  Medusa
2  -  Gigle
3  -  Brozi
Sign up to request clarification or add additional context in comments.

Comments

0

Make the bottom part:

deck = []

with open('dets.txt') as fh:
    for line in fh:
        row = line.strip().split(', ')
        deck.append(Card(*row))

Comments

0

You are using list and it doesn't have any method as get. Try using a dictionary

It'll help you retrieving class object

ex.

myDict = {card_id1 : CardObject1, card_id2 : CardObject2, ....}

So when you want to get object of a card, just type this

if card_id in myDict.keys():
    myDict(card_id)

Comments

0

You could simplify your code a lot, and also make it more Python by doing the following:

  1. Use with open('dets.txt') as f to read your file. You should close a file when you open it, and this context manager allows you to close it no matter what happense while you are reading your file.
  2. Cosidering that the order of the parameters per line is the same order of the arguments you defined in your init method you can use a Python feature called list unpacking.

So you, you can use the following code:

import sys
import os
class Card:
    def__init__(self, card_id, name, desc, hp):
        self.card_id = card_id
        self.name = name
        self.desc = desc
        self.hp = hp
        self.cards = []

deck = []

with open('dets.txt') as f:
    for line in f:
        args = line.strip().split(', ')
        args[0] = int(args[0])
        args[-1] = int(args[-1])
        deck.append(Card(*args))

Considering you are using Python 3.7+

You can use dataclases to simplify your class definition as follows:

@dataclass
class Card:
    card_id: int
    name: str
    desc: str
    hp: int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.