Search code examples
python-3.xtic-tac-toe

Function clear's everything on output


The last time the "for loop" in main calls board I want the print to remain

import ast
import subprocess
import platform

def clear():
    subprocess.Popen( "cls" if platform.system() == "Windows" else "clear", shell=True)
    return

def pBoard(navn1, navn2, liste):
  print("       1     2     3 ")  
  print("    ----------------- ")  
  print("   1| ",liste[0][0]," | ",liste[0][1]," | ",liste[0][2]," | ")
  print("    ----------------- ")
  print("   2| ",liste[1][0]," | ",liste[1][1]," | ",liste[1][2]," | ")
  print("    ----------------- ")
  print("   3| ",liste[2][0]," | ",liste[2][1]," | ",liste[2][2]," | ")   
  print("    ----------------- ")


def hVunnet():
  print("Hei")

def lTrekk():
   print("Hei")

def iD():
  kor1, kor2 = ast.literal_eval(input("Ditt trekk:"))

def nBrukere():
  navn1 = input("Hva er navnet til bruker 1? : ")
  navn2 = input("Hva er navnet til bruker 2? : ")
  return navn1, navn2


def main():
  liste = [[0 for x in range(3)] for x in range(3)] 
  navn1, navn2 = nBrukere()
  for x in range (0,3):
    clear()
    pBoard(navn1, navn2, liste)

main()

Thx in advance.


Solution

  • You need to wait for the subprocess to exit before continuing on to print the last board. Alternatively, learn what bytes to output to the terminal to clear it (I don't know them offhand) and just do that in your program instead of executing another process for that.

    The simplest way is to use subprocess.run() instead of subprocess.Popen().