2013-10-30 75 views
0

我試着用py_compile編譯,但我得到這個錯誤:語法錯誤:(Unicode的錯誤)「unicodeescape」編解碼器不能解碼位置2-3字節:截斷\ UXXXXXXXX逃脫編譯PY到PYC錯誤

這是我的編譯代碼:

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

這是相關代碼:

#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) 
+3

你能告訴我們你的相關的代碼?錯誤不是很困難。 – aIKid

+3

嘗試字符串前puttin一個'r',如'R 「d:\大學項目\ Python的\ EXAMPLES \ XO遊戲\ XO.py」' –

回答

3

它在你pycompile.compile()通話。對於你的情況,使用原始字符串:

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

或寫入路徑時的另一種標準的方式是始終使用雙反斜線:

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

你需要逃避你的反斜線,把\\,你有\。這是因爲反斜槓通常會引入特殊字符,例如\n用於換行符,或者代碼以\U爲Unicode字符。在使用\U的地方,編譯器需要一個正確的unicode字符,而不是\University

或者,您也可以使用/而不是\,Windows也理解這一點。

4

您可以逃脫你的角色,或者你可以告訴你正在處理原始字符串蟒蛇,就像這樣:

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

@aIKid噢,胡說:P –

0

RemcoGerlich是正確的,但我會建議,而不是更換開始通過R」其關閉反斜槓的特殊解釋雖然這主要是用於正則表達式也有很多個反斜槓路徑規範的雙引號,它也適用於Windows的文件名優勢:只有一個字符插入,而不是許多改變或使用「錯誤的」路徑parator。

+0

但要注意,如果你有'\ ''在字符串中,或​​者字符串以'\\'結尾。 – RemcoGerlich