关于python:如何让我的用户输入从我的课程运行?

How can I get my user input to run from my class?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()


drawObj = SimpleDraw需改为drawObj = SimpleDraw()

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()


我想猜猜你想干什么。首先,让我们重写这段代码,我将解释我们进行时所做的更改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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:
 %s"
% ("
"
.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


在此行中:

1
drawObj = SimpleDraw

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

1
drawObj = SimpleDraw() # note parentheses

你不需要你的SimpleDraw类中的__init__;你从Turtle继承这个。通常情况下,将第一个参数调用到实例方法self,而不是bob!例如:

1
2
3
4
5
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方法:

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

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

1
2
3
4
if number == 0:
    ...
elif number == 1:
    ...