3

My problem is that I have a text file which contains this:

♦J
♣J
♠J
♣8
♦A
♥9
♥J
♥J
♦A
♦K
♦7
♦J
♦7
♦A
♦K
♥7
♣10
♣J
♠A
♦A
♣J
♦7
♠10
♥K
♣9
♥10
♦A
♠8
♠J
♥9
♦8
♠A

And I am trying to append this in to 8 different lists in order from to bottom. I have 8 different lists and a list that contains all the different lists they are set like this:

deckOne = []
deckTwo = []
DeckThree = []
DeckFour = []
deckFive = []
deckSix = []
deckSeven = []
deckEight = []
deckList = [deckOne, deckTwo, DeckThree, DeckFour, deckFive, deckSix, deckSeven, deckEight]

I get no errors when I run the program, but, I do not get the output that I am expecting. the expected output would look like this:

[♦J, ♣J, ♠J, ♣8]
[♦A, ♥9, ♥J, ♥J]
[♦A, ♦K, ♦7, ♦J]
[♦7, ♦A, ♦K, ♥7]
[♣10, ♣J, ♠A, ♦A]
[♣J, ♦7, ♠10, ♥K]
[♣9, ♥10, ♦A, ♠8]
[♠J, ♥9, ♦8, ♠A]

But the result I am getting is currently this:

[['♦J', '♦A', '♣10', '♣9'], ['♣J', '♦K', '♣J', '♥10'], ['♠J', '♦7', '♠A', '♦A'], ['♣8', '♦J', '♦A', '♠8'], ['♦A', '♦7', '♣J', '♠J'], ['♥9', '♦A', '♦7', '♥9'], ['♥J', '♦K', '♠10', '♦8'], ['♥J', '♥7', '♥K', '♠A']]

This output is wrong is because it appends every 4 lines in the file instead of 4 lines to one list, then the next 4 to the next list, and so on. the code I am trying to use to do this, is this:

def hentLagretSpill():
    import codecs
    with codecs.open("game_save.txt", 'rb', encoding="utf-8") as gameGetter:
        count = 0
        for i, l in enumerate(gameGetter):
            count += 1
            deckList[i % 8].append(l.strip())
        print(deckList)

But I have also tried this, and it works, but it is very lengthy:

        x = -1
        for line in gameGetter:
            x += 1
            if x // 4 == 0:
                deckOne.append(line.strip('\n'))
            if x // 4 == 1:
                deckTwo.append(line.strip('\n'))
            if x // 4 == 2:
                DeckThree.append(line.strip('\n'))
            if x // 4 == 3:
                DeckFour.append(line.strip('\n'))
            if x // 4 == 4:
                deckFive.append(line.strip('\n'))
            if x // 4 == 5:
                deckSix.append(line.strip('\n'))
            if x // 4 == 6:
                deckSeven.append(line.strip('\n'))
            if x // 4 == 7:
                deckEight.append(line.strip('\n'))

How would I solve this?

1
  • 1
    Now this is a good question! You have included your input data and both the actual and desired output data. You have included two versions of your code, no matter that one of them doesn't quite work. Well done. Commented Nov 15, 2019 at 12:16

1 Answer 1

4

The problem you are seeing is that the % (modulo) operator returns the remainder, not the integer division as in your second example. You can fix it by going back to the integer division, and using that as the index in the list of lists.

def hentLagretSpill():
    with open("game_save.txt", 'r', encoding="utf-8") as gameGetter:
        for i, l in enumerate(gameGetter):
            ix = i // 4
            deckList[ix].append(l.strip())
        print(deckList)

# prints:
[['♦J', '♣J', '♠J', '♣8'],
 ['♦A', '♥9', '♥J', '♥J'],
 ['♦A', '♦K', '♦7', '♦J'],
 ['♦7', '♦A', '♦K', '♥7'],
 ['♣10', '♣J', '♠A', '♦A'],
 ['♣J', '♦7', '♠10', '♥K'],
 ['♣9', '♥10', '♦A', '♠8'],
 ['♠J', '♥9', '♦8', '♠A']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.