Python Turtle Drawing Assignment - 分配变量时出现问题

时间:2017-05-24 23:38:13

标签: python variables turtle-graphics

我将在几个小时内完成作业,而且我不知道该做什么。 我正在使用python 3。

我一直在尝试运行代码,但是我遇到了错误。在这一点上,我想我会寻求帮助。

提前谢谢你!

说明:http://people.uncw.edu/cferner/Classes/csc231/Assignment1.html

 import turtle
 import random


class myRectangle:

def __var__(self):
    self.__x
    self.__y
    self.__width
    self.__height
    self.__color

def __init__(self, x, y, width, height, color):
    self.__x = x
    self.__y = y
    self.__width = width
    self.__height = height
    self.__color = color

def getX(self):
    return self.__x

def setX(self, x):
    self.__x = x

def getY(self):
    return self.__y

def setY(self, y):
    self.__y = y

def getWidth(self):
    return self.__width

def setWidth(self, width):
    self.__width = width

def getHeight(self):
    return self.__height

def setHeight(self, height):
    self.__height = height

def getColor(self):
    return self.__color

def setColor(self, color):
    self.__color = color

def draw(self, turtle):
    turtle.penup()
    turtle.goto(self.getX(), self.getY())
    turtle.heading(0)
    turtle.pencolor("blue")
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(self.__width())
    turtle.left(90)
    turtle.forward(self.__height())
    turtle.left(90)
    turtle.forward(self.__width())
    turtle.left(90)
    turtle.forward(self.__height())
    turtle.left(90)
    turtle.end_fill()


def main():

wn = turtle.TurtleScreen
mr = myRectangle
phil = turtle.Turtle()
macie = turtle.Turtle()
sashi = turtle.Turtle()
roxie = turtle.Turtle()
darla = turtle.Turtle()
sammy = turtle.Turtle()

phil.pencolor("blue")
phil.pensize(7)

listofcolors = ("blue", "red", "yellow", "orange", "pink", "green")
c = random.choice(listofcolors)

xy = random.randrange(-200, 201)
wh = random.randrange(1, 151)

for i in [phil, macie, sashi, roxie, darla, sammy]:
    mr.setColor(c)
    mr.setX(xy)
    mr.setY(xy)
    mr.setWidth(wh)
    mr.setHeight(wh)





main()

错误:

     /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 
    "/Users/Desktop/OneDrive - UNC-Wilmington/UNCW Files/Summer 
      2017/CSC 231/Assignment 1/turtleRectangle.py"
    Traceback (most recent call last):
    File "/Users/Desktop/OneDrive - UNC-Wilmington/UNCW Files/Summer 
   2017/CSC 231/Assignment 1/turtleRectangle.py", line 102, in <module>
    main()
     File "/Users/Desktop/OneDrive - UNC-Wilmington/UNCW Files/Summer 
  2017/CSC 231/Assignment 1/turtleRectangle.py", line 92, in main
   mr.setColor()
   TypeError: setColor() missing 2 required positional arguments: 
  'self' and 'color'

   Process finished with exit code 1

Download link for Python file

2 个答案:

答案 0 :(得分:0)

你的测试代码很乱,你不需要多只乌龟,只需要一条,就行了:

def main():

    listofcolors = ("blue", "red", "yellow", "orange", "pink", "green")
    wn = turtle.Screen()
    mr = myRectangle(0, 0, 100, 100, "blue")
    phil = turtle.Turtle()

    for _ in range(6):
        x = random.randint(-200, 200)
        print(x)
        y = random.randint(-200, 200)
        print(y)
        w = random.randint(1, 150)
        print(w)
        h = random.randint(1, 150)
        print(h)
        c = random.choice(listofcolors)
        print(c)

        mr.setX(x)
        mr.setY(y)
        mr.setWidth(w)
        mr.setHeight(h)
        mr.setColor(c)

        mr.draw(phil)

    wn.mainloop()

main()

这会让您按照说明进入第7步。

我不知道你的__var__()函数应该做什么,这似乎是一个冗长的无操作。您的draw()功能略有调整:

    def draw(self, turtle):
        turtle.penup()
        turtle.goto(self.getX(), self.getY())
        turtle.setheading(0)
        turtle.color(self.getColor())  # set both pen & fill color
        turtle.pendown()
        turtle.begin_fill()
        turtle.forward(self.__width)
        turtle.left(90)
        turtle.forward(self.__height)
        turtle.left(90)
        turtle.forward(self.__width)
        turtle.left(90)
        turtle.forward(self.__height)
        turtle.left(90)
        turtle.end_fill()

答案 1 :(得分:-1)

问题是您在帖子中粘贴的代码与您列出的文件中的代码不同。在文件中,第92行有一个错误,你没有向setColor提供任何参数:

db:structure:dump