代码无法正常工作。作为新的编码人员,这只是一小件事:
import turtle
import random
me=turtle.Turtle()
def up_right():
me.right(90)
me.forward(100)
def down_right():
me.right(90)
me.forward(100)
choose = (up_right(), down_right)
random.choice(chose)
它应该选择一个,但要同时选择两个。
我已经尝试过random.sample
和random.choice
,但无法使它们正常工作。
答案 0 :(得分:2)
除了choose
的错字...我的建议是,在创建一个函数元组并使用random.choice()
选择该函数之后,应调用{{ 1}}。
random.choice()
答案 1 :(得分:0)
我发现您的代码存在三个问题。前两个人已经指出了choose
与chose
的错字,并在将其称为函数upright
时将parens()留在了(up_right(), down_right)
上。
第三点是up_right
和down_right
都执行相同的动作,因此即使其余代码工作正常,您也看不到任何区别!下面是解决此问题的重写:
from turtle import Screen, Turtle
from random import choice
def up_right(turtle):
turtle.setheading(90)
turtle.forward(100)
def down_right(turtle):
turtle.setheading(270)
turtle.forward(100)
choices = [up_right, down_right]
screen = Screen()
me = Turtle('turtle')
choice(choices)(me)
screen.mainloop()
运行几次,您有时会看到乌龟在屏幕上抬头,有时它在屏幕上下来。