问题描述
我目前正在编写一段 python 海龟代码,它要求我在我的正方形内随机放置圆圈。出于某种原因,它们总是放在对角线上,不会随机绘制。
import random
import turtle
for i in range(15):
# draws 15 circles and then stops
random_position = random.randint(-80,90)
# allows the computer to pick any number between -80 and 90 as an x or y co-ordinate
pit_x = random_position
pit_y = random_position
pit_radius = 7
# radius of circle
turtle.penup()
turtle.setposition(pit_x,pit_y-pit_radius)
turtle.pendown()
turtle.begin_fill()
turtle.circle(pit_radius)
turtle.end_fill()
turtle.hideturtle()
# the circle is Now a black hole
它是这样的:
我该如何解决这个问题?
解决方法
pit_x
和 pit_x
是相等的。改为这样做:
pit_x = random.randint(-80,90)
pit_y = random.randint(-80,90)
,
也许用
pit_x = random.randint(-80,90)
或者如果你想要一个函数(我想你想为每个函数做 random_pos) 只是
def randomPitNumber():
return random.randint(-80,90)
然后
pit_x = randomPitNumber()
pit_y = randomPitNumber()
并删除 random_position
变量。
提示:为了帮助纠正某些人回答的答案,请按旁边的复选标记,以便其他人将其标记为正确答案。