【问题标题】:How can I get my user input to run from my class? [closed]如何让我的用户输入从我的班级运行? [关闭]
【发布时间】:2014-04-09 09:47:06
【问题描述】:
bob = Turtle() 
bob.shape("turtle")


class SimpleDraw(Turtle):
   def __init__(Turtle):
        bob = Turtle
   def draw_square(bob, length):
       '''This function draws a regular square'''
       for i in range(4):
           bob.forward(length)
           bob.left(90)


   def draw_polygon(bob, n, length):
       '''This function draws a regular polygon'''
       angle = 360.0 / n
       for i in range(n):
           bob.forward(length)
           bob.left(angle)


   def draw_circle (t, r):

      circumference = 2 * math.pi * r
      n = 50
      length = circumference / n
      polygon (t, n, length)    


drawObj = SimpleDraw

drawObj.draw_polygon   
drawObj.draw_circle 
drawObj.draw_square


def testSimpleDraw():
   number = raw_input('Please choose a draw option: \
   1-draw square \
   2-draw polygon \
   3-draw circle \
   0-to exit');
   if number == 0:
       exit()
   else:
       if number == 1: 
           drawObj.draw_square(bob, 5)

       else:
           if number == 2:
               drawObj.draw_polygon(bob, 7, 70)

           else:
               if number == 3: 
                   drawObj.draw_circle(50, 50)


if __name__ == '__main__':
   testSimpleDraw()

【问题讨论】:

  • 你到底想做什么? Turtle 是什么?
  • @hkpeprah Turtle 来自turtle 库。至于重置,耸耸肩

标签: python python-2.7 turtle-graphics


【解决方案1】:

drawObj = SimpleDraw需要改为drawObj = SimpleDraw()

这就是您实例化类的方式。您正在将 SimpleDraw 类分配给 drawObj - drawObj 与 SimpleDraw 类相同,而不是代码中的 SimpleDraw 实例。

【讨论】:

    【解决方案2】:

    在这一行:

    drawObj = SimpleDraw
    

    您将 SimpleDraw 分配给名称 drawObj。相反,您应该创建一个要分配的类的实例

    drawObj = SimpleDraw() # note parentheses
    

    你的SimpleDraw 类中不需要__init__;你从Turtle 继承这个。习惯上调用实例方法的第一个参数self,而不是bob!例如:

    def draw_square(self, length):
        '''This function draws a regular square'''
        for i in range(4):
            self.forward(length)
            self.left(90)
    

    您需要调整您的draw_circle 方法以正确调用draw_polygon 方法:

    def draw_circle(self, r, n=50):
        circumference = 2 * math.pi * r
        length = circumference / n
        self.draw_polygon(n, length)
    

    请注意,您可以使用elif 来简化您的代码:

    if number == 0:
        ...
    elif number == 1:
        ...
    

    【讨论】:

      【解决方案3】:

      我将猜测您要做什么。首先,让我们重写这段代码,我将解释这些变化。

      class SimpleDraw(Turtle):
          # No __init__ need, calls __init__ of subclass implicitly
          # So self refers to an instance of Turtle
          # Since all your code essentially makes use of the same thing, lets create
          # a generic base draw.
          def draw_generic(self, angle, length, n):
              for i in range(n):
                  self.forward(length)
                  self.left(angle)
      
          def draw_square(self, length):
              # Notice self instead of bob, this is how you refer to the instance
              # of the class that has been created.
              """
              This function draws a regular square.
              """
              self.draw_generic(90, length, 4) # Note, self implicitly passed
      
          def draw_polygon(self, n, length):
              """
              This function draws a regular polygon
              """
              angle = 360.0 / n
              self.draw_generic(angle, length, n)
      
          def draw_circle(self, t, r):
              circumference = 2 * math.pi * r
              n = 50
              length = circumference / n
              self.draw_polygon(t, n, length) # Note: draw_polygon instead of polygon
      
      
      def test_simple_draw():
          # Note the lowercase and underscores, this preferred by PEP 8
          # http://stackoverflow.com/questions/8908760/should-i-use-camel-case-or-underscores-in-python
          options = ["1 - draw square", "2 - draw polygon", "3 - draw circle", "0 -exit"]
          msg = "Please choose a draw option:\n %s" % ("\n".join(options))
          # Note that number is a string, so we have to try and coerce it to an int, and
          # validate this as well.
          number = None
          while number is None:
              try:
                  number = raw_input(msg)
                  number = int(number)
                  if 0 > number or 3 < number:
                      raise ValueError # Not a valid option
              except ValueError: # If the coercion fails
                  print "%s is not a valid option.  Please re-enter." % number
                  number = None # Set to None so we loop again
      
          # Now we have the number, and it's actually a number.
          # You make a lot of use of else, which can be replaced by elif, which is the
          # like else but specifies a condition; a union of if and else
          drawObj = SimpleDraw() # We create our object here, note braces for instantiation
          if number == 0:
              exit()
          elif number == 1:
              drawObj.draw_square(5)
          elif number == 2:
              drawObj.draw_polygon(7, 70)
          else: # We can have confidence in this value because of our checks above
              drawObj.draw_circle(50, 50)
      
      
      if __name__ == "__main__":
          test_simple_draw()
      

      现在这段代码理解了您最初想要的想法,但利用了您最初尝试使用的类继承。
      更多参考来源:
      http://docs.python.org/2/tutorial/classes.html

      【讨论】:

        【解决方案4】:
        import math
        import random
        import turtle
        
        class SimpleTurtle(turtle.Turtle):
            # There is no need to override the default __init__
        
            # Also, making the self argument 'bob' is just irritating -
            # everyone expects to see 'self', why mess us up?
        
            def draw_polygon(self, num_sides, side_length):
                """
                Draw an n-sided regular polygon
                """
                angle = 360. / num_sides
                for side in range(num_sides):
                    self.forward(side_length)
                    self.left(angle)
        
            def draw_square(self, side_length):
                """
                Draw a square
                """
                self.draw_polygon(4, side_length)
        
            def draw_circle(self, radius, num_sides=60):
                """
                Draw a circle
                """
                angle = 360. / num_sides
                side_length = 2. * radius * math.sin(math.radians(angle))
                self.draw_polygon(num_sides, side_length)
        
        def get_int(prompt, lo=None, hi=None):
            while True:
                try:
                    val = int(raw_input(prompt))
                    if (lo is None or lo <= val) and (hi is None or val <= hi):
                        return val
                except ValueError:
                    pass
        
        def do_menu(prompt, options):
            print(prompt)
            for i,opt in enumerate(options, 1):
                print(" {:>2}: {}".format(i, opt))
            return get_int("? ", 1, len(options)) - 1
        
        def test_SimpleTurtle():
            bob = SimpleTurtle(shape="turtle")
            while True:
                choice = do_menu("Please choose a draw option:", ["draw a square", "draw a polygon", "draw a circle", "quit"])
                if choice == 0:
                    side_length = random.randint(10, 50)
                    bob.draw_square(side_length)
                elif choice == 1:
                    sides = random.randint(5, 12)
                    side_length = random.randint(6, 30)
                    bob.draw_polygon(sides, side_length)
                elif choice == 2:
                    radius = random.randint(8, 40)
                    bob.draw_circle(radius)
                else:
                    break
        
        if __name__ == '__main__':
            test_SimpleTurtle()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-28
          • 1970-01-01
          • 2018-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多