2016-08-31 6 views
1
#!/usr/bin/python 
import turtle 

class GoToCommand: 
    def __init__(self,x,y,width=1,color="black"): 
     self.x = x 
     self.y = y 
     self.color = color 
     self.width = width 

    def draw(self,turtle): 
     turtle.width(self.width) 
     turtle.pencolor(self.color) 
     turtle.goto(self.x,self.y) 

class CircleCommand: 
    def __init__(self,radius, width=1,color="black"): 
     self.radius = radius 
     self.width = width 
     self.color = colorO 

    def draw(self,turtle): 
     turtle.width(self.width) 
     turtle.pencolor(self.color) 
     turtle.circle(self.radius) 

class BeginFillCommand: 
    def __init__(self,color): 
     self.color = color 

    def draw(self,turtle): 
     turtle.fillcolor(self.color) 
     turtle.begin_fill() 

class EndFillCommand: 
    def __init__(self): 
     pass 

    def draw(self,turtle): 
     turtle.end_fill() 

class PenUpCommand: 
    def __init__(self): 
     pass 

    def draw(self,turtle): 
     turtle.penup() 

class PenDownCommand: 
    def __init__(self): 
     pass 

    def draw(self,turtle): 
     turtle.pendown() 

class PyList: 
    def __init__(self): 
     self.items = [] 

    def append(self,item): 
     self.items = self.items + [item] 

    def __iter__(self): 
     for c in self.items: 
      yield c 

def main(): 
    filename = input("Insert File Name: ") 

    t = turtle.Turtle() 
    screen = t.getscreen() 
    file = open(filename, "r") 


    graphicsCommands = PyList() 

    command = file.readline().strip() 

    while command != "": 

     if command == "goto": 
      x = float(file.readline()) 
      y = float(file.readline()) 
      width = float(file.readline()) 
      color = file.readline().strip() 
      cmd = GoToCommand(x,y,width,color) 

     elif command == "circle": 
      radius = float(file.readline()) 
      width = float(file.readline()) 
      color = file.readline().strip() 
      cmd = CircleCommand(radius,width,color) 

     elif command == "beginfill": 
      color = file.readline().strip() 
      cmd = BeginFillCommand(color) 

     elif command == "endfill": 
      cmd = EndFillCommand() 

     elif command == "penup": 
      cmd = PenUpCommand() 

     elif command == "pendown": 
      cmd = PenDownCommand() 
     else: 

     # printed. 
      raise RuntimeError("Unknown Command: " + command) 

      graphicsCommands.append(cmd) 

      command = file.readline().strip() 

     for cmd in graphicsCommands: 
      cmd.draw(t) 

      file.close() 
      t.ht() 
      screen.exitonclick() 
      print("Program Execution Completed.") 

    if __name__ == "__main__": 

     main() 

私は、移動、追加、削除する必要があるものを探しました。まだ正しく動作していません。メインのようなものは存在しません。Python:エラーなしでスクリプトを実行します。何もしません。

+1

'__name__ ==場合は、 "__ __main":' ')(DEF主'内部ではない、トップレベルにする必要があります。 – Barmar

+1

'main __'関数内に' if __name__ == '__main __' 'ブロックをインデントしました。 – user2357112

+0

他の字下げの問題があります。最後のループで 'file.close()'は誤ってインデントされています。 – Elazar

答えて

2

インデントエラー、 以下のように修正....

 print("Program Execution Completed.") 

if __name__ == "__main__": 

    main() 
+1

はい、それは厄介でした。あなたのコメントを提出する前にそれを変更しました:) – David

関連する問題