在 Python 中尝试移动 Turtle 时键入错误
Type Error when trying to move Turtle in Python
我尝试移动乌龟到随机位置来画星星,但是当我 运行 代码时,我得到:
Traceback (most recent call last): File "so_quick_run.py", line 36,
in
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
我想我遇到这个问题是因为我的 turtle 的 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()
正如@DYZ 提到的那样,您没有 return 在 rng()
中输入任何内容,只是 return 您生成的随机数:
#Randon Number Generator
import random
def rng():
for i in range(1):
return random.randint(-250,250)
我尝试移动乌龟到随机位置来画星星,但是当我 运行 代码时,我得到:
Traceback (most recent call last): File "so_quick_run.py", line 36, in
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
我想我遇到这个问题是因为我的 turtle 的 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()
正如@DYZ 提到的那样,您没有 return 在 rng()
中输入任何内容,只是 return 您生成的随机数:
#Randon Number Generator
import random
def rng():
for i in range(1):
return random.randint(-250,250)