如何使函数一遍又一遍地进行随机事件

发布于 2025-02-08 15:11:36 字数 259 浏览 0 评论 0原文

我的意思是,当我声明函数

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2025-02-15 15:11:36
import turtle
import random
turt = turtle.Turtle()

def randomspot():
    turt.penup()
    turt.goto(
      random.randint(-300,300),
      random.randint(-300,300)
    )
    turt.pendown()

您需要在定义X和Y之前,在运行X和Y之前,您需要创建一个新的随机数,然后运行相同的功能。 X和Y永远没有机会改变。

import turtle
import random
turt = turtle.Turtle()

def randomspot():
    turt.penup()
    turt.goto(
      random.randint(-300,300),
      random.randint(-300,300)
    )
    turt.pendown()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文