i have this weird bug i must say or might not.. im just a beginner btw. I can obviously look up to the internet for the solution but i dont want to because to be honest this should work right but the problem on this is on the output after 4 moves my checkwin functions got declared when none of my if statement hit True:
def checkwin(boards):
if boards[0] and boards[3] and boards[6] == 'x':
print('Theres a winner')
elif boards[0] and boards[4] and boards[8] == 'x':
print('Theres a winner')
elif boards[0] and boards[1] and boards[2] == 'x':
print('Theres a winner')
elif boards[2] and boards[5] and boards[8] == 'x':
print('Theres a winner')
elif boards[6] and boards[7] and boards[8] == 'x':
print('Theres a winner')
elif boards[3] and boards[4] and boards[5] == 'x':
print('Theres a winner')
elif boards[1] and boards[4] and boards[7] == 'x':
print('Theres a winner')
elif boards[6] and boards[4] and boards[2] == 'x':
print('Theres a winner')
else:
print('still no winner')
main code:
def player1(boards):
on = True
print("player 1 you are {}".format(ran1))
while on:
try:
playerone = (input("where to put:"))
num = int(playerone) - 1
if num >= 9:
print('you exceeded only 1-9')
continue
elif boards[num] == '-':
boards[num] = ran1
elif boards[num] != '-':
print('Its already played!')
continue
except ValueError:
print('please print numbers only')
continue
on = False
displayboard(boards)
def player2(boards):
on = True
print('playertwo you are {}'.format(ran2))
while on:
try:
playertwo = input('player two where to put:')
num = int(playertwo) - 1
if num >= 9:
print('you are {} and be sure number is 1-9 only'.format(ran2))
continue
elif boards[num] == '-':
boards[num] = ran2
elif boards[num] != '-':
print('Its already played!')
continue
else:
print('Its already played!')
continue
except ValueError:
print('please print numbers only')
continue
on = False
displayboard(boards)
def displayboard(boards):
print(boards[0] + "|" + boards[1] + "|" +boards[2])
print(boards[3] + "|" + boards[4] + "|" +boards[5])
print(boards[6] + "|" + boards[7] + "|" + boards[8])
def main():
on = True
while on:
player1(board)
player2(board)
checkwin(board)
then after i play like 4 moves or even 3 moves it declares theres a winner which is weird because ive never establish it on checkwinner function
output:
x|o|x #[0,1,2,3,4,5,6,7,8]
o|-|-
-|-|-
theres a winner
to be honest i can just look up on the internet and copy paste from them because it is much neater but my mind just cant move on from these cause i dont know what happens wrong