hamidfzm
hamidfzm

Reputation: 4685

compiling py to pyc error

I tried to compile using py_compile but I get this error: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

And this is my compiling code:

py_compile.compile("D:\University Project\Python\Examples\XO Game\XO.py")

this is the relevant code:

#difining libraries
import turtle
from tkinter import messagebox as msgbox

#defining constants
osc=xsc=asc=0#scores vars

np="n.gif"
op="o.gif"
xp="x.gif"
pturn=xp
turtle.register_shape(np)
turtle.register_shape(op)
turtle.register_shape(xp)

t=[turtle.Turtle() for x in range(10)]
for i in range(9):
    t[i].ht()

arra=[[0]*3 for x in range(3)]#array for holding scores
tempa=[[0]*3 for x in range(3)]#array for holding scores
temparray=[[0]*3 for x in range(3)]#array for holding scores


def tur0(x,y):
    tclick(0)
def tur1(x,y):
    tclick(1)
def tur2(x,y):
    tclick(2)
def tur3(x,y):
    tclick(3)
def tur4(x,y):
    tclick(4)
def tur5(x,y):
    tclick(5)
def tur6(x,y):
    tclick(6)
def tur7(x,y):
    tclick(7)
def tur8(x,y):
    tclick(8)


def drawTable():#creating buttons and table
    turtle.ht()
    turtle.width(5)
    turtle.color("black")
    xd=-133
    yd=133
    for i in range(9):
        t[i].st()
        t[i].shape(np)
        t[i].up()
        t[i].goto(xd,yd)
        xd+=133
        if xd==2*133:
            xd=-133
            yd-=133
    xd=-64
    yd=64
    for i in range(2):
        turtle.up()
        turtle.goto(-197.5,yd)
        turtle.setheading(0)
        turtle.down()
        turtle.fd(394)
        yd-=128
        turtle.up()
        turtle.goto(xd,197.5)
        turtle.setheading(270)
        turtle.down()
        turtle.fd(394)
        xd+=128


def inarray():#put scores in array
    global arra
    for i in range(3):
        for j in range(3):
            arra[i][j]=0
    xd=0
    yd=0
    for i in range(9):
        if t[i].shape()==np:
            arra[xd][yd]=0
        elif t[i].shape()==xp:
            arra[xd][yd]=1
        elif t[i].shape()==op:
            arra[xd][yd]=2
        yd+=1
        if yd==3:
            yd=0
            xd+=1
def grestart():
    if msgbox.askyesno(title="The End", message="Game finished. Do you want to restart?")==True:
        turtle.clear()
        drawTable()
    else:
        quit()


#these are for checking winner
def check(i,j,k):
    a=t[i].shape()+t[j].shape()+t[k].shape()
    turtle.color("green")
    turtle.width(5)
    turtle.up()
    if a==xp+xp+xp:
        turtle.goto(t[i].pos())
        turtle.down()
        turtle.goto(t[k].pos())
        turtle.up()
        p=msgbox.showinfo(title="Congratulations!", message="Player X is Winner!")
        grestart()
    elif a==op+op+op:
        turtle.goto(t[i].pos())
        turtle.down()
        turtle.goto(t[k].pos())
        turtle.up()
        p=msgbox.showinfo(title="Failed! :(", message="Player O is Winner!")
        grestart()

def ccheck():
    check(0,1,2)
    check(3,4,5)
    check(6,7,8)
    check(0,3,6)
    check(1,4,7)
    check(2 ,5,8)
    check(0 ,4,8)
    check(2 ,4,6)
#---------------------------
def chturn():
    global pturn
    if pturn==xp:
        pturn=op
    else:
        pturn=xp


def xWayT():#player x ways Temperary
    global temparray
    global xsc
    xsc=0
    '''for i in range(3):
        if (temparray[i][0]==1 or temparray[i][0]==0) and (temparray[i][1]==1 or temparray[i][1]==0) and (temparray[i][2]==1 or temparray[i][2]==0):
            xsc+=1
    for i in range(3):
        if (temparray[0][i]==1 or temparray[0][i]==0) and (temparray[1][i]==1 or temparray[1][i]==0) and (temparray[2][i]==1 or temparray[2][i]==0):
            xsc+=1'''
    if (temparray[0][0]==1 or temparray[0][0]==0) and (temparray[0][1]==1 or temparray[0][1]==0) and (temparray[0][2]==1 or temparray[0][2]==0):
        xsc+=1
    if (temparray[1][0]==1 or temparray[1][0]==0) and (temparray[1][1]==1 or temparray[1][1]==0) and (temparray[1][2]==1 or temparray[1][2]==0):
        xsc+=1
    if (temparray[2][0]==1 or temparray[2][0]==0) and (temparray[2][1]==1 or temparray[2][1]==0) and (temparray[2][2]==1 or temparray[2][2]==0):
        xsc+=1
    if (temparray[0][0]==1 or temparray[0][0]==0) and (temparray[1][0]==1 or temparray[1][0]==0) and (temparray[2][0]==1 or temparray[2][0]==0):
        xsc+=1
    if (temparray[0][1]==1 or temparray[0][1]==0) and (temparray[1][1]==1 or temparray[1][1]==0) and (temparray[2][1]==1 or temparray[2][1]==0):
        xsc+=1
    if (temparray[0][2]==1 or temparray[0][2]==0) and (temparray[1][2]==1 or temparray[1][2]==0) and (temparray[2][2]==1 or temparray[2][2]==0):
        xsc+=1
    if (temparray[0][0]==1 or temparray[0][0]==0) and (temparray[1][1]==1 or temparray[1][1]==0) and (temparray[2][2]==1 or temparray[2][2]==0):
        xsc+=1
    if (temparray[0][2]==1 or temparray[0][2]==0) and (temparray[2][2]==1 or temparray[2][2]==0) and (temparray[2][0]==1 or temparray[2][0]==0):
        xsc+=1

def oWayT():#player o ways Temperary
    global temparray
    global osc
    osc=0
    '''for i in range(3):
        if (temparray[i][0]==2 or temparray[i][0]==0) and (temparray[i][1]==2 or temparray[i][1]==0) and (temparray[i][2]==2 or temparray[i][2]==0):
            osc+=1
    for i in range(3):
        if (temparray[0][i]==2 or temparray[0][i]==0) and (temparray[1][i]==2 or temparray[1][i]==0) and (temparray[2][i]==2 or temparray[2][i]==0):
            osc+=1
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][2]==2 or temparray[0][2]==0) and (temparray[2][2]==2 or temparray[2][2]==0) and (temparray[2][0]==2 or temparray[2][0]==0):
        osc+=1'''
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[0][1]==2 or temparray[0][1]==0) and (temparray[0][2]==2 or temparray[0][2]==0):
        osc+=1
    if (temparray[1][0]==2 or temparray[1][0]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[1][2]==2 or temparray[1][2]==0):
        osc+=1
    if (temparray[2][0]==2 or temparray[2][0]==0) and (temparray[2][1]==2 or temparray[2][1]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[1][0]==2 or temparray[1][0]==0) and (temparray[2][0]==2 or temparray[2][0]==0):
        osc+=1
    if (temparray[0][1]==2 or temparray[0][1]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[2][1]==2 or temparray[2][1]==0):
        osc+=1
    if (temparray[0][2]==2 or temparray[0][2]==0) and (temparray[1][2]==2 or temparray[1][2]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][2]==2 or temparray[0][2]==0) and (temparray[2][2]==2 or temparray[2][2]==0) and (temparray[2][0]==2 or temparray[2][0]==0):
        osc+=1

def omove(a,b):
    global temparray,tempa
    if (temparray[0][0]==2) and (temparray[0][1]==2) and (temparray[0][2]==2):
        tempa[a][b]=-100
    if (temparray[1][0]==2 ) and (temparray[1][1]==2) and (temparray[1][2]==2):
        tempa[a][b]=-100
    if (temparray[2][0]==2) and (temparray[2][1]==2) and (temparray[2][2]==2):
        tempa[a][b]=-100
    if (temparray[0][0]==2) and (temparray[1][0]==2) and (temparray[2][0]==2):
        tempa[a][b]=-100
    if (temparray[0][1]==2) and (temparray[1][1]==2) and (temparray[2][1]==2):
        tempa[a][b]=-100
    if (temparray[0][2]==2) and (temparray[1][2]==2) and (temparray[2][2]==2):
        tempa[a][b]=-100
    if (temparray[0][0]==2) and (temparray[1][1]==2) and (temparray[2][2]==2):
        tempa[a][b]=-100
    if (temparray[0][2]==2) and (temparray[2][2]==2) and (temparray[2][0]==2):
        tempa[a][b]=-100
    '''for i in range(3):
        if (temparray[i][0]==2) and (temparray[i][1]==2) and (temparray[i][2]==2):
            tempa[a][b]=-100
    for i in range(3):
        if (temparray[0][i]==2) and (temparray[1][i]==2) and (temparray[2][i]==2):
            tempa[a][b]=-100
    if (temparray[0][0]==2) and (temparray[1][1]==2) and (temparray[2][2]==2):
        tempa[a][b]=-100
    if (temparray[0][2]==2) and (temparray[2][2]==2) and (temparray[2][0]==2):
        tempa[a][b]=-100'''

def xmove(a,b):
    global temparray,tempa
    if (temparray[0][0]==1) and (temparray[0][1]==1) and (temparray[0][2]==1):
        tempa[a][b]=-100
    if (temparray[1][0]==1 ) and (temparray[1][1]==1) and (temparray[1][2]==1):
        tempa[a][b]=-100
    if (temparray[2][0]==1) and (temparray[2][1]==1) and (temparray[2][2]==1):
        tempa[a][b]=-100
    if (temparray[0][0]==1) and (temparray[1][0]==1) and (temparray[2][0]==1):
        tempa[a][b]=-100
    if (temparray[0][1]==1) and (temparray[1][1]==1) and (temparray[2][1]==1):
        tempa[a][b]=-100
    if (temparray[0][2]==1) and (temparray[1][2]==1) and (temparray[2][2]==1):
        tempa[a][b]=-100
    if (temparray[0][0]==1) and (temparray[1][1]==1) and (temparray[2][2]==1):
        tempa[a][b]=-100
    if (temparray[0][2]==1) and (temparray[2][2]==1) and (temparray[2][0]==1):
        tempa[a][b]=-100
    '''for i in range(3):
        if (temparray[i][0]==1) and (temparray[i][1]==1) and (temparray[i][2]==1):
            tempa[a][b]=-100
    for i in range(3):
        if (temparray[0][i]==1) and (temparray[1][i]==1) and (temparray[2][i]==1):
            tempa[a][b]=-100
    if (temparray[0][0]==1) and (temparray[1][1]==1) and (temparray[2][2]==1):
        tempa[a][b]=-100
    if (temparray[0][2]==1) and (temparray[2][2]==1) and (temparray[2][0]==1):
        tempa[a][b]=-100'''

def aip(i,j):#ai process function
    global xsc,osc,asc,temparray,tempa
    if temparray[i][j]==0:
        temparray[i][j]=2
        xWayT()#calling player x ways
        oWayT()#calling player o ways
        print(xsc,",",osc)
        asc=xsc-osc
        tempa[i][j]=asc
        omove(i,j)
        temparray[i][j]=1
        xmove(i,j)
        temparray[i][j]=0

def fmin():#find the minimum score for machines move
    global tempa
    xd=-1
    yd=-1
    '''xd2=0
    yd2=0'''
    mini=100
    print(tempa)
    for i in range(3):
        for j in range(3):
            if (tempa[i][j]<mini) and (tempa[i][j]!=0):
                mini=tempa[i][j]
                print(mini)
                xd=i
                yd=j
    if xd == 0 and yd == 0:
        t[0].shape(op)
    if xd == 0 and yd == 1:
        t[1].shape(op)
    if xd == 0 and yd == 2:
        t[2].shape(op)
    if xd == 1 and yd == 0:
        t[3].shape(op)
    if xd == 1 and yd == 1 :
        t[4].shape(op)
    if xd == 1 and yd == 2 :
        t[5].shape(op)
    if xd == 2 and yd == 0:
        t[6].shape(op)
    if xd == 2 and yd == 1 :
        t[7].shape(op)
    if xd == 2 and yd == 2 :
        t[8].shape(op)
    '''for i in range(9):
        if xd==xd2 and yd==yd2:
            t[i].shape(op)
        yd2+=1
        if yd2==3:
            yd2=0
            xd2+=1'''
    ccheck()
    chturn()

def payoffda():#readung temperory arrays and starting part of ai processing
    global temparray,tempa,arra
    for i in range(3):
        for j in range(3):
            temparray[i][j]=arra[i][j]
            tempa[i][j]=0
    for i in range(3):
        for j in range(3):
            aip(i,j)


def tclick(i):
    if t[i].shape()==np:
        t[i].shape(xp)
        inarray()
        ccheck()
        chturn()
        payoffda()
        fmin()

#logo showing
logo="Hamid's XO Game"
turtle.colormode(255)
turtle.ht()

turtle.up()
turtle.goto(70-((len(logo)/2)*30),0)
turtle.down()
turtle.delay(150)
for i in range(255,0,-15):
    turtle.pencolor(255,i,i)
    turtle.write(logo,align="left",font=("Tahoma", 30, "normal"))

for i in range(0,255,15):
    turtle.pencolor(255,i,i)
    turtle.write(logo,align="left",font=("Tahoma", 30, "normal"))
t[9]    
turtle.delay(0)
turtle.clear()
drawTable()
t[0].onclick(tur0)
t[1].onclick(tur1)
t[2].onclick(tur2)
t[3].onclick(tur3)
t[4].onclick(tur4)
t[5].onclick(tur5)
t[6].onclick(tur6)
t[7].onclick(tur7)
t[8].onclick(tur8)

Upvotes: 1

Views: 1875

Answers (4)

guidot
guidot

Reputation: 5333

RemcoGerlich is right, but I would recommend instead to replace the beginning double quote of the path specification by r" which switches off the special interpretation of backslashes. While this is mostly used for regular expressions also having lots of backslashes, it also works for Windows filenames. Advantage: Only one character to insert instead of many changes or using the "wrong" path separator.

Upvotes: 0

aIKid
aIKid

Reputation: 28272

It's in your pycompile.compile() call. For your case, use raw string:

py_compile.compile(r"D:\University Project\Python\Examples\XO Game\XO.py")

Or the another standard way is to always use double backslashes when writing paths:

py_compile.compile("D:\\University Project\\Python\\Examples\\XO Game\\XO.py")

Upvotes: 5

Nafiul Islam
Nafiul Islam

Reputation: 82470

You can escape your characters or you can tell python that you're dealing with a raw string, like so:

py_compile.compile(r"D:\University Project\Python\Examples\XO Game\XO.py")

Upvotes: 5

RemcoGerlich
RemcoGerlich

Reputation: 31260

You need to escape your backslashes, put \\ where you have \. That is because backslash usually introduces special characters, like \n for a newline, or codes starting with \U for unicode characters. Where you use \U, the compiler expects a correct unicode character, not \University.

Alternatively you can also use / instead of \, Windows understands that too.

Upvotes: 3

Related Questions