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

时间:2018-09-28 19:39:38

标签: python turtle-graphics

我试图移动到乌龟到随机的地方画星星,但是当我运行代码时,我得到了:

  

回溯(最近一次通话最后一次):文件“ 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”,   1689行,转到       self._goto(Vec2D(* x))TypeError:*之后的类型对象参数必须是序列,而不是NoneType

我认为我是通过乌龟的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()

1 个答案:

答案 0 :(得分:0)

如@DYZ所述,您不会在rng()中返回任何内容,而只是返回您生成的随机数:

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