2013-04-23 140 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)