Def function being called when it isnt supposed to

First off, I want to say thank you to everyone so far who has helped me with this! You all have been very helpful and patient.

So, the def that is being called is on line 42, def winspeak(player,XOwin): #winner. This def is to be called on line 10, def checkstatus(listXO,playerwin,XOwin):. When checkstatus() finds that one of the players has won, it tells winspeak() to tell the user who won, how many points each player has, and ask if the user wants to play again. This works perfectly.

The problem comes when there is a tie, which is on lines 30-40. When there is a tie, it seems that somehow winspeak() gets called despite there being no way for it to.

Thank you all… again. lol.

Here is my code.

Link: Tic-Tac-Toe
or

win = "h"
sheet = "|1|2|3|\n|4|5|6|\n|7|8|9|"
turn = 0
xscore = 0
oscore = 0
gone = []
listx = []
listo = []

def checkstatus(listXO,playerwin,XOwin):
  global win, sheet, gone, listx, listo
  if len(listXO) >= 3:
    if 1 in listXO and 2 in listXO and 3 in listXO:
      winspeak(playerwin,XOwin) #vertical
    elif 4 in listXO and 5 in listXO and 6 in listXO:
      winspeak(playerwin,XOwin) #vertical
    elif 7 in listXO and 8 in listXO and 9 in listXO:
      winspeak(playerwin,XOwin) #vertical
    elif 1 in listXO and 4 in listXO and 7 in listXO:
      winspeak(playerwin,XOwin) #horizontal
    elif 2 in listXO and 5 in listXO and 8 in listXO:
      winspeak(playerwin,XOwin) #horizontal
    elif 3 in listXO and 6 in listXO and 9 in listXO:
      winspeak(playerwin,XOwin) #horizontal
    elif 1 in listXO and 5 in listXO and 9 in listXO:
      winspeak(playerwin,XOwin) #diagonal
    elif 3 in listXO and 5 in listXO and 7 in listXO:
      winspeak(playerwin,XOwin) #diagonal

    if (len(listx) + len(listo)) == 9: #draw
      print("\n Neither player wins, thats a tie!")
      print("Player 1:",xscore,"\nPlayer 2:",oscore)
      keep = input("Continue? (y/n)\n > ")
      if keep == "y":
        win = "h"
        sheet = "|1|2|3|\n|4|5|6|\n|7|8|9|"
        gone = []
        multigame()
      elif keep == "n":
        exit()

def winspeak(player,XOwin): #winner
  global win, sheet, gone, xscore, oscore
  win == XOwin
  print("\n"+player+" wins!\n")

  if player == "Player 1":
    xscore += 1
  elif player == "Player 2":
    oscore += 1
  print("Player 1:",xscore,"\nPlayer 2:",oscore)
  keep = input("Continue? (y/n)\n > ")
  if keep == "y":
    win = "h"
    sheet = "|1|2|3|\n|4|5|6|\n|7|8|9|"
    gone = []
    multigame()
  elif keep == "n":
    exit()

def play(XO,player,listXO):
  global sheet, listx, listo
  while True:
    num = input(player+" enter an available number where you want to put an "+XO+".\n > ")
    try:
      num = int(num)
    except ValueError:
      print("Not a valid number, try again.")
      continue
    if num < 1 or num > 9:
      print("Number not in correct range, try again.")
      continue
    if num in gone:
      print("Number already used, try again.")
      continue
    gone.append(num)
    listXO.append(num)
    #print(listXO)
    sheet = sheet.replace(str(num),XO)
    print(sheet)
    break

def game(): #one game
  global gone, listx, listo
  win = "h"
  sheet = "|1|2|3|\n|4|5|6|\n|7|8|9|"
  print(sheet)
  turn = 0
  gone = []
  listx = []
  listo = []
  while True:
    while win != "Xwin" or win != "Owin":
      if turn % 2 == 0:
        play("X","Player 1",listx)
        checkstatus(listx,"Player 1","Xwin")
      elif turn % 2 == 1:
        play("O","Player 2",listo)
        checkstatus(listo,"Player 2","Owin")
      turn += 1

def multigame(): #many games
  while True:
    game()

multigame()
1 Like

Why do you think that function is being called? What behavior are you seeing that you want to change?

1 Like

I know its being called because when I make the outcome to be a draw, it prints…

Player 1 wins!
Player 1: 1
Player 2: 0
Continue? (y/n)

Instead, I want after checkstatus() is trying to find who won and it finds that it was a draw (which is seen on line 30). It will then tell the user that is was a draw, the score of each player, and then ask if the user wants to continue playing. It doesnt do that. The only place where it can print that a player has won, is in winspeak(), which can only be called if a player has won. It tells if someone has won on lines 12-28.

When I try the code you’ve posted, I don’t see that behavior. Are you sure you’re running the same version?

 > 6
|X|O|X|
|O|O|X|
|X|X|O|

 Neither player wins, thats a tie!
Player 1: 0
Player 2: 0
Continue? (y/n)
 >
1 Like

Okay, thats weird. I just tested it too just like you did, I got a tie.

However, the one ive been doing is this one.

> 9
|X|O|X|
|O|X|O|
|O|X|X|

Player 1 wins!

Player 1: 1 
Player 2: 0
Continue? (y/n)

WAIT IM STUPID

im sorry… XD

No need for sorrow. You’re a winner! You created a program that works as it should. :smiley:

1 Like

Thank you, that makes me feel better :slight_smile:

1 Like