我在尝试完成python龟程序时遇到了麻烦。当我尝试输入x和y坐标以及函数的半径值时 t.drawSnowman(x = 25,y = 25,radius = 25)当我输入值时,程序行为不正常。但如果我省略上面的参数而只是使用t.drawSnowman(),程序按预期工作,但我不是能够创建雪人的各种实例。
我真的很想帮忙试图找出如何输入参数并仍然具有程序功能。
这是我的代码
import turtle
class MyTurtle(turtle.Turtle):
""""""
def __init__(self):
"""Turtle Constructor"""
turtle.Turtle.__init__(self, shape="turtle")
def drawNose(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draw nose '''
self.pendown()
self.goto(-(radius) * (0.25),(radius * 6)+(radius * 4)+(radius))
self.goto(0,(radius * 6)+(radius * 4)+(radius)+(radius * (0.25)))
self.goto((radius) * (0.25),(radius * 6)+(radius * 4)+(radius))
self.goto(0,(radius * 6)+(radius * 4)+(radius))
self.penup()
def leftEye(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draw left eye'''
self.pendown()
self.circle(radius*(.25))
self.penup()
def rightEye(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draw right eye'''
self.pendown()
self.circle(radius*(.25))
self.penup()
def bottomOfHat(self, x=0, y=0, radius = 15, circle_color = "black"):
''' draw the long part of the hat at the bottom '''
self.goto(0,(radius * 6)+(radius * 4)+(radius * 2))
self.pendown()
self.goto(-(radius),(radius * 6)+(radius * 4)+(radius * 2))
self.goto(-(radius),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
self.goto(radius,(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
self.goto(radius,(radius * 6)+(radius * 4)+(radius * 2))
self.goto(0,(radius * 6)+(radius * 4)+(radius * 2))
self.penup()
def topOfHat(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draw the top bigger part of the hat'''
self.goto(radius*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
self.pendown()
self.goto(radius*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * 2))
self.goto(-(radius)*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * 2))
self.goto(-(radius)*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
def bottomCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draws the bottom circle'''
self.pendown()
self.circle(radius * 3)
self.penup()
def middleCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draw the middle circle'''
self.pendown()
self.circle(radius * 2)
self.penup()
def topCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
'''draw the top circle'''
self.pendown()
self.circle(radius)
self.penup()
def drawSnowman(self, x=0, y=0, radius = 15, circle_color = "black"):
self.bottomCircle()
self.goto(0,radius * 6)
self.middleCircle()
self.goto(0,(radius * 6)+(radius * 4))
self.topCircle()
self.goto(-(radius) * (0.5),(radius * 6)+(radius * 4)+(radius))
self.leftEye()
self.goto((radius) * (0.5),(radius * 6)+(radius * 4)+(radius))
self.rightEye()
self.goto(0,(radius * 6)+(radius * 4)+(radius))
self.drawNose()
self.bottomOfHat()
self.topOfHat()
t = MyTurtle()
t.hideturtle()
radius = 15
t.drawSnowman(x = 25,y = 25,radius = 25)
这是我使用参数时雪人的照片 t.drawsnowman(x = 25 y = 25 radius = 25)
当我输入没有参数时,这是雪人t.drawsnowman()答案 0 :(得分:2)
尝试一步一步地找出代码实际执行的操作(或使用调试器)。
t.drawSnowman()
,radius
将15
。 bottomCircle()
,其中将绘制一个半径为radius * 3
= 45
radius * 6
= 90
(您现在“在圈子的顶部”)middleCircle()
这将是一个半径为radius * 2
= 30
的圆圈现在,看看如果使用参数调用函数会发生什么:
t.drawSnowman(x = 25,y = 25,radius = 25)
,radius
将25
。 bottomCircle()
,其中会绘制一个半径为radius * 3
= 45
的圆圈。请注意局部变量radius
是15
的方式,因为它是该参数的默认值,并且您没有将传递给drawSnowman
的值传递给bottomCircle
方法radius * 6
= 150
(您现在远离的圆圈)middleCircle()
,其中半径为radius * 2
= 30
的圆圈。请注意局部变量radius
也是15
。所以,问题在于你传递了radius
的值,但是你只使用该值来移动drawSnowman
中的乌龟,而不是在任何其他函数中移动乌龟。