Python 用蟒蛇画星星
我需要写一个函数,根据这个人想要多少星星的问题来绘制随机的星星。a=颜色,b=长度,c,d是起始坐标。我真的不确定我会错在哪里。任何提示、提示或帮助都将不胜感激Python 用蟒蛇画星星,python,turtle-graphics,Python,Turtle Graphics,我需要写一个函数,根据这个人想要多少星星的问题来绘制随机的星星。a=颜色,b=长度,c,d是起始坐标。我真的不确定我会错在哪里。任何提示、提示或帮助都将不胜感激 import turtle from random import* def star(a,b,c,d): x=5 y=0 turtle.color(a) turtle.begin_fill() turtle.penup() turtle.goto(c,d) turtle.pendo
import turtle
from random import*
def star(a,b,c,d):
x=5
y=0
turtle.color(a)
turtle.begin_fill()
turtle.penup()
turtle.goto(c,d)
turtle.pendown()
while x>0:
turtle.forward(b)
turtle.right(144)
turtle.forward(b)
x-=1
turtle.end_fill()
star('red',100,0,0)
def random_color():
randvar=randrange(0,5)
if randvar==0:
return ('red')
elif randvar==1:
return ('blue')
elif randvar==2:
return ('green')
elif randvar==3:
return ('yellow')
else:
return ('black')
def length():
randvar=randrange(5,71)
def x():
randvar=randrange(-280,281)
def y():
randvar=randrange(-200,201)
def night_sky():
z=int(input('How many stars do you want?'))
a=random_color
b=length
c=x
d=y
while z>0:
star(a,b,c,d)
z-=1
要调用函数,请在函数名后加括号:
a=random_color()
b=length()
c=x()
d=y()
确保在脚本末尾调用
night\u sky()
。目前,只有
star('red',100,0,0)
有人打电话来。这就是为什么你只能看到一颗星
函数
length
、x
和y
需要使用return
。否则,默认情况下将返回None
def length():
return randrange(5,71)
def x():
return randrange(-280,281)
def y():
return randrange(-200,201)
您需要将定义
a
、b
、c
和d
的语句移动到while
-循环中,以免同一颗星被绘制z
次。当我们使用时,While
-循环可以更简单地写成for
-循环:
for i in range(z):
a=random_color()
b=length()
c=x()
d=y()
star(a,b,c,d)
如果使用更具描述性的变量名,代码将变得更具自文档性:
def star(color, side_length, x, y):
print(color, side_length, x, y)
turtle.color(color)
turtle.begin_fill()
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(5):
turtle.forward(side_length)
turtle.right(144)
turtle.forward(side_length)
turtle.end_fill()
因此,通过这些更改,代码变为:
import turtle
import random
def star(color, side_length, x, y):
print(color, side_length, x, y)
turtle.color(color)
turtle.begin_fill()
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(5):
turtle.forward(side_length)
turtle.right(144)
turtle.forward(side_length)
turtle.end_fill()
def random_color():
randvar = randrange(0, 5)
if randvar == 0:
return ('red')
elif randvar == 1:
return ('blue')
elif randvar == 2:
return ('green')
elif randvar == 3:
return ('yellow')
else:
return ('black')
def length():
return random.randrange(5, 71)
def xcoord():
return random.randrange(-280, 281)
def ycoord():
return random.randrange(-200, 201)
def night_sky():
z = int(input('How many stars do you want?'))
for i in range(z):
color = random_color()
side_length = length()
x = xcoord()
y = ycoord()
star(color, side_length, x, y)
night_sky()
嘿,通常人们喜欢更集中的问题。你应该告诉我们你的计划会有什么结果,以及你期望会有什么结果。当您共享代码时,请确保您只共享解释错误所需的代码。添加到这个问题的一个好消息可能是它产生了什么(或代码抛出的错误或异常)的图片同上。但是,非常感谢您(1)已经尝试编写程序,而不是仅仅要求其他人来编写,以及(2)将代码包含在您的问题中。许多询问者(新的和不太新的)不做这些事情+1分。这些都是有效的分数(我相信你通过你的SO分数知道)。但是在运行程序时(通过将源代码复制到IDEL中),代码会画出一颗星(尽管没有询问)。乌龟似乎愿意为自己的身体承担一项功能inputs@JasonSperske:单星由语句
star('red',100,0,0)
绘制。请注意,这些参数不是函数对象。如果尝试调用night_sky()
,并且在a
、b
、c
和d
的定义中不使用括号,则会出现错误。Python将函数视为一级对象。。。可以将作为参数传递,但它们的含义与添加括号时得到的返回值完全不同。@unutbu,完全忽略了这一点。我的错