简体   繁体   English

尝试在 Python 中移动 Turtle 时输入错误

[英]Type Error when trying to move Turtle in Python

I'm try to move to turtle to random places to draw a star but when I run the code I get :我尝试将乌龟移动到随机位置以绘制星星,但是当我运行代码时,我得到:

Traceback (most recent call last): File "so_quick_run.py", line 36, in回溯(最近一次调用):文件“so_quick_run.py”,第 36 行,在

main() File "so_quick_run.py", line 34, in main move() File "so_quick_run.py", line 28, in move alex.goto(rng(), rng()) File

"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 1689, in goto self._goto(Vec2D(*x)) TypeError: type object argument after * must be a sequence, not NoneType "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 1689, in goto self._goto(Vec2D(*x)) TypeError: type object * 后的参数必须是序列,而不是 NoneType

I think I'm getting this problem from using an RNG from my turtle's goto command.我想我从乌龟的 goto 命令中使用 RNG 来解决这个问题。

#Import turtle
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()

#Turtle Setting
alex.speed(10)
alex.color("yellow")
wn.bgcolor("black")
wn.screensize(600,600)

#Drawing star
def star(alex):
  for x in range(5):
      alex.pendown()
      alex.forward(50)
      alex.right(144)
      alex.penup()

#Randon Number Generator
import random
def rng():
  for i in range(1):
    random.randint(-250,250)

#Moving turtle
def move():
    alex.penup()
    alex.goto(rng(), rng())
    alex.pendown()

#Main funaton
def main():
    for i in range(10):
        move()
        star(alex)
main()

#Ending the loop
wn.mainloop()

As @DYZ mentioned, you're not returning anything in rng() just return the random number that you generate:正如@DYZ 所提到的,您没有在rng()返回任何内容,只是返回您生成的随机数:

#Randon Number Generator
import random
def rng():
    for i in range(1):
        return random.randint(-250,250)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM