I recently started learning programming and I am not really good at it. Today, it tried to program the simple game "TicTacToe". But I got an error in the section where I figure out who winner is and I really don't what I'm doing wrong. Since I'm a beginner, I probably made also lots of mistakes concerning the structure (functions,classes, etc.) and the readability of my code, so feel free to give me valuable feedback :) I hope it's not too messy
The typical error I get is: invalid syntax
Here is the code:
# This is how the board looks like:
# L1|M1|R1 D1 (Diagonal): L1,M2,R3
# L2|M2|R2 D2 (Diagonal) L3,M2,R1
# L3|M3|R3
data = {}
tiles= ("L1","L2","L3","M1","M2","M3","R1","R2","R3")
print("This is the board:")
print("L1|M1|R1")
print("L2|M2|R2")
print("L3|M3|R3\n")
p1 = str(input("Player 1, please enter your name: "))
p2 = str(input("Player 2, please enter your name: "))
while True:
if len(data) == 9: #when all tiles have been chosen but there is no winner
print("It seems like a draw!")
break
while True: #enter tile player1
input_p1 = str(input(p1 + ", please choose a tile: "))
if input_p1 in data or input_p1 not in tiles:
print("\This tile has already been chosen or does not exist. Try again")
continue
data[input_p1] = p1
break
while True: #enter tile player2
input_p2 = str(input(p2 + ", please choose a tile: "))
if input_p2 in data or input_p2 not in tiles:
print("\nThis tile has already been chosen or does not exist. Try again")
continue
data[input_p2] = p2
break
#Check, if Player1 has won:
if (data["L1"] == p1 and data["L2"] == p1 and data["L3"] == p1) or (data["M1"] == p1 and data["M2"] == p1 and data["M3"] == p1) or (data["R1"] == p1 and data["R2"] == p1 and data["R3"] == p1) or (data["L1"] == p1 and data["M2"] == p1 and data["R3"] == p1) or (data["L3"] == p1 and data["M2"] == p1 and data["R1"] == p1):
print("We have a winner: " + p1)
#Check, if Player2 has won:
elif (data["L1"] == p2 and data["L2"] == p2 and data["L3"] == p2) or (data["M1"] == p2 and data["M2"] == p2 and data["M3"] == p2) or (data["R1"] == p2 and data["R2"] == p2 and data["R3"] == p2) or (data["L1"] == p2 and data["M2"] == p2 and data["R3"] == p2) or (data["L3"] == p2 and data["M2"] == p2 and data["R1"] == p2):
print("We have a winner: " + p2)
Also: I don't know how to better phrase the part below ^^
error:
This is the board:
L1|M1|R1
L2|M2|R2
L3|M3|R3
Player 1, please enter your name: Player1
Player 2, please enter your name: Player2
Player1, please choose a tile: M1
Player2, please choose a tile: L2
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-8-9e2fcd740bfb> in <module>
44
45 #Check, if Player1 has won:
---> 46 if (data["L1"] == p1 and data["L2"] == p1 and data["L3"] == p1) or (data["M1"] == p1 and data["M2"] == p1 and data["M3"] == p1) or (data["R1"] == p1 and data["R2"] == p1 and data["R3"] == p1) or (data["L1"] == p1 and data["M2"] == p1 and data["R3"] == p1) or (data["L3"] == p1 and data["M2"] == p1 and data["R1"] == p1):
47 print("We have a winner: " + p1)
48
KeyError: 'L1'