2013-04-23 86 views
-2

我試圖運行我的python腳本並獲取無效的語法錯誤我已經仔細檢查了問題,仍然不能看到問題可以看到問題嗎?如果有人能幫助看到問題,下面是代碼。python無效的語法錯誤

謝謝。

import random 
import Tkinter 

class life (Tkinter.Frame): 

    def __init__(self,x,y): 
     Tkinter.Frame.__init__(self) 
     self.master.title("The Game Of Life") 
     self.grid = [[False for j in range (y)] for i in range (x)] 
     universe = Tkinter.Frame(self) 
     def createButton (i,j): 
      bitmap=None 
      if self.grid[i][j]:bitmap ='gray75' 
      gridEntry = Tkinter.Button(universe, bitmap=bitmap) 
      gridEntry.grid(row=i, column =j) 
     self._applyToEachCellOfGrid (createButton) 
     universe.pack(side='top') 
     scale= Tkinter.Scale(self, orient = Tkinter.HORIZONTAL, from_=1, to =10, 
          command=self.setPeriod) 
     scale.pack(side='top') 
     quit=Tkinter.Button(self, text='Quit', command=self.quit) 
     quit.pack(side='left') 
     run=Tkinter.Button(self, text='Run' , command=self.run) 
     run.pack(side='right') 
     pause=Tkinter.Button(self, text='Pause', command=self.pause) 
     pause.pack(side='right') 
     self.pack() 

    def _applyToEachCellOfGrid (self, function) : 
     for i in range (len(self.grid)): 
      for j in range (len(self.grid[i])): 
       function(i,j) 

    def populateGridRandomly(self,x): 
     random.seed() 
     def setRandomly(i,j): 
      if random.random()<x:self.grid[i][j]= True 
     self._applyToEachCellOfGrid(setRandomly) 

    def calculateNextGeneration(self): 
     newGrid=[[ False for j in range (len (self.grid[i]))] for i in range (len (self.grid))] 
     def calculateLiveness(x,y): 
      count = 0 
      for i in range (x-1, x+2): 
       if 0 <=i<len (self.grid[i]): 
        if ((i !=x)or(j !=y)) and self.grid[i][j]:count +=1 
      if self.grid[x][y]:return 2 <= count <=3 
      else: return count ==3 
     def setNewGrid(x,y): 
      newGrid[x][y]= calculateLiveness(x,y) 
     self._applyToEachCellOfGrid (setNewGrid) 
     self.grid = newGrid 

    def pause(self): 
     print 'pause called.' 

    def run(self): 
     print 'run called' 

    def setPeriod (self, value): 
     print 'setPeriod called with value' , value 

if __name__ =="__main__': 
    game=Life(8,10) 
    game.mainloop() 
+1

而錯誤信息? – 2013-04-23 18:13:56

+1

錯字底部:應該是「__main__」而不是「__main__」 – Johnny 2013-04-23 18:15:45

+0

allender,當我運行模塊時出現「無效語法錯誤」 – user1957509 2013-04-23 18:16:14

回答

5

如果您使用的開口",你必須使用一個閉合"爲好,不'

File "synt2.py", line 63 
    if __name__ =="__main__': 
          ^
SyntaxError: EOL while scanning string literal 
+0

並且這不是他得到的唯一語法錯誤 – Johnny 2013-04-23 18:17:18

+0

@Johnny:還有其他的錯誤,但在糾正這個錯誤之後,我在Python 2中沒有得到語法錯誤。你指的是什麼? – DSM 2013-04-23 18:17:56

+0

Nam eError:name'Life'未定義(行64) – Johnny 2013-04-23 18:18:55

0

這裏是你的代碼,工作。

import random 
import Tkinter 

class Life (Tkinter.Frame): 

    def __init__(self,x,y): 
     Tkinter.Frame.__init__(self) 
     self.master.title("The Game Of Life") 
     self.grid = [[False for j in range (y)] for i in range (x)] 
     universe = Tkinter.Frame(self) 
     def createButton (i,j): 
      bitmap=None 
      if self.grid[i][j]:bitmap ='gray75' 
      gridEntry = Tkinter.Button(universe, bitmap=bitmap) 
      gridEntry.grid(row=i, column =j) 
     self._applyToEachCellOfGrid (createButton) 
     universe.pack(side='top') 
     scale= Tkinter.Scale(self, orient = Tkinter.HORIZONTAL, from_=1, to =10, 
          command=self.setPeriod) 
     scale.pack(side='top') 
     quit=Tkinter.Button(self, text='Quit', command=self.quit) 
     quit.pack(side='left') 
     run=Tkinter.Button(self, text='Run' , command=self.run) 
     run.pack(side='right') 
     pause=Tkinter.Button(self, text='Pause', command=self.pause) 
     pause.pack(side='right') 
     self.pack() 

    def _applyToEachCellOfGrid (self, function) : 
     for i in range (len(self.grid)): 
      for j in range (len(self.grid[i])): 
       function(i,j) 

    def populateGridRandomly(self,x): 
     random.seed() 
     def setRandomly(i,j): 
      if random.random()<x:self.grid[i][j]= True 
     self._applyToEachCellOfGrid(setRandomly) 

    def calculateNextGeneration(self): 
     newGrid=[[ False for j in range (len (self.grid[i]))] for i in range (len (self.grid))] 
     def calculateLiveness(x,y): 
      count = 0 
      for i in range (x-1, x+2): 
       if 0 <=i<len (self.grid[i]): 
        if ((i !=x)or(j !=y)) and self.grid[i][j]:count +=1 
      if self.grid[x][y]:return 2 <= count <=3 
      else: return count ==3 
     def setNewGrid(x,y): 
      newGrid[x][y]= calculateLiveness(x,y) 
     self._applyToEachCellOfGrid (setNewGrid) 
     self.grid = newGrid 

    def pause(self): 
     print 'pause called.' 

    def run(self): 
     print 'run called' 

    def setPeriod (self, value): 
     print 'setPeriod called with value' , value 

if __name__ =="__main__": 
    game=Life(8,10) 
    game.mainloop() 
0

你宣佈一個名爲life類,而你打電話Life。 Python區分大小寫。將第4行更改爲class Life (Tkinter.Frame):或將第64行更改爲game=life(8,10)