如何使函数一遍又一遍地进行随机事件
我的意思是,当我声明函数
import turtle
import random
turt = turtle.Turtle()
x = random.randint(-300,300)
y = random.randint(-300,300)
def randomspot():
turt.penup()
turt.goto(x,y)
turt.pendown()
和声明功能两次时,它会转到同一位置。我应该怎么办?
What I mean is when I declare a function like
import turtle
import random
turt = turtle.Turtle()
x = random.randint(-300,300)
y = random.randint(-300,300)
def randomspot():
turt.penup()
turt.goto(x,y)
turt.pendown()
and declear the function twice, it goes to the same spot. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在定义X和Y之前,在运行X和Y之前,您需要创建一个新的随机数,然后运行相同的功能。 X和Y永远没有机会改变。
you need to create a new random number every time you run the function, before you were defining x and y then running the same function. x and y never have a chance to change.