Current status: I’m working on exercise 41 of 52 in Learn Python the Hard Way, and continuing from Make Three in a Row, Part 1 (though I’m probably not far enough in the book for this yet…)


Replace list cause I’ve now learnt about dictionary 📚

moves = { }

for i in range(0,9):
    moves[i] = input(">>> ")
    print("Player X or 0 said:", moves[i])

print(moves) # let’s look at that dictionary!

If players didn’t need to take turns… 🤪

…then something like this would (sort of but not really) differentiate between two players:

moves = { }

for i in range(0,9,2):
        moves[i] = input("X >>> ")
        print("Player X:", moves[i])

for i in range(1,10,2):
        moves[i] = input("0 >>> ")
        print("Player 0:", moves[i])

print(moves)

Hmmmmmmmmmmmmm. Does this mean it’s easier to use a while loop? Let’s try.

moves = { }
i = 1

while i <= 9:
    moves[i] = input(">>> ")
    print(f"Player X or 0 said: {moves[i]}")
    i += 1

print(f"{moves}")

And this means I can finally differentiate between the players properly!

moves = { }
i = 1

while i <= 8:
    moves[i] = input("X >>> ")
    print(f"Player X: {moves[i]}")
    i += 1
    moves[i] = input("0 >>> ")
    print(f"Player 0: {moves[i]}")
    i += 1

print(f"{moves}")

That will only count 8 moves since the loop increments twice. But I’ll go with that for now since the last one is given anyway… So now I’ve got a script that does this:

  • asks for input 8 times (but doesn’t care what you type)
  • creates a dictionary with the user input
  • has no clue about any rules or who wins

I couldn’t figure out how to inch forward from that 👆. Dictionary seems like a good idea, but for now; I’m simplifying back to a list — and playing around with using other Python parts I’ve learnt.

Define a function, and also…

append is a method I can use on the list data type
len() is a built-in function to return length

def game():

    moves = [ ]

    while len(moves) < 8: # Count number of items in the list 'moves'
        playermove = input("X >>> ")
        print(f"Player X: {playermove}")
        moves.append(playermove) # Add item 'playermove' to end of list
        playermove = input("0 >>> ")
        print(f"Player 0: {playermove}")
        moves.append(playermove)

    print(f"{moves}")

game()

And what if I try to use some if statements?!

def game():
    print("Lets play! Only integers between 1-9 plz:")

    for playermove in range(0,9):
        playermove = int(input(">>> "))

        if playermove == 1:
            print("Top left corner, all right. ↖️")
        if playermove == 2:
            print("Top center. ⬆️")
        if playermove == 3:
            print("Top right corner, nice. ↗️")
        if playermove == 4:
            print("Left center. ⬅️")
        if playermove == 5:
            print("Yeah that’s a good one. 🆒")
        if playermove == 6:
            print("Right center. ➡️")
        if playermove == 7:
            print("Bottom left corner, good job. ↙️")
        if playermove == 8:
            print("Bottom center. ⬇️")
        if playermove == 9:
            print("Bottom right corner, cool. ↘️")

game()

Well, that was fun. But back to the book! The current chapter is “Learning to Speak Object-Oriented”, and starting to get a grip on what objects and classes are will hopefully help me next.