我想知道为什么我的代码会继续打印' tan'我似乎无法打印出真正的赢家。
import turtle
import random
turtles = []
def setup():
global turtles
startline = -610
screen = turtle.Screen()
screen.bgpic('pavement.gif')
screen.setup(1290, 720)
turtle_ycor = [-40, -20, 0, 20, 40]
turtle_color = ['pink', 'skyblue', 'indigo', 'turquoise', 'tan']
for i in range(0, len(turtle_ycor)):
new_turtle = turtle.Turtle()
new_turtle.shape('turtle')
new_turtle.penup()
new_turtle.setpos(startline, turtle_ycor[i])
new_turtle.color(turtle_color[i])
new_turtle.pendown()
turtles.append(new_turtle)
def race():
global turtles
winner = False
finishline = 550
while not winner:
for current_turtle in turtles:
move = random.randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor()
if (xcor >= finishline):
winner = True
current_turtle.forward(0)
turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
setup()
race()
turtle.mainloop()
我尝试了winner_color[0]
。
答案 0 :(得分:0)
我想我可能在这段代码中发现了错误。
我发现,使用您的代码,列表turtle_color
中的最后一个字符串值总是获胜。
这是因为代码的这一部分:
while not winner:
for current_turtle in turtles:
move = random.randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor() #this should be indented, or itll only run this code
#for the last color in the list, in this case, tan
if (xcor >= finishline): #and all of this code should be indented too
winner = True #so it checks all colors, not just tan
current_turtle.forward(0)
turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
因此,正确的代码(完整)是:
import turtle
import random
turtles = []
def setup():
global turtles
startline = -610
screen = turtle.Screen()
screen.bgpic('pavement.gif')
screen.setup(1290, 720)
turtle_ycor = [-40, -20, 0, 20, 40]
turtle_color = ['pink', 'skyblue', 'indigo', 'turquoise', 'tan']
for i in range(0, len(turtle_ycor)):
new_turtle = turtle.Turtle()
new_turtle.shape('turtle')
new_turtle.penup()
new_turtle.setpos(startline, turtle_ycor[i])
new_turtle.color(turtle_color[i])
new_turtle.pendown()
turtles.append(new_turtle)
def race():
global turtles
winner = False
finishline = 550
while not winner:
for current_turtle in turtles:
move = random.randint(0, 10)
current_turtle.forward(move)
xcor = current_turtle.xcor()
if (xcor >= finishline):
winner = True
current_turtle.forward(0)
turtle.forward(0)
winner_color = current_turtle.color()
print('The winner is', winner_color[1])
setup()
race()
告诉我是否还有任何错误(等等)!
答案 1 :(得分:-2)
胜利者实际上可能是“晒黑”。在每场比赛结束时。
原因可能是random.seed尚未调用。因此,代码将在每次运行时使用相同的种子进行初始化,从而导致每次具有相同的获胜者时生成相同的随机数序列。您可以通过例如在每次调用之前初始化种子或仅在代码顶部初始化种子来增加随机化。
添加以下行:
random.seed() # initializes seed using current system time
代码中的任何位置都应该随机化结果。
关于读出winner_color的说明:我假设
winner_color = current_turtle.color() # gets the color of the current turtle
现在是一个包含颜色' tan'的字符串。在这种情况下,索引[0]将对字符串起作用,而不是列表。参见例如
>>> a = 'tan'
>>> a[0]
't'
>>> a[1]
'a'
>>> a[2]
'n'
>>> a[:]
'tan'
>>> a[::-1]
'nat'
另外,请以一种很好的方式查看如何present您的问题(stackoverflow文本编辑器还显示样式的工具提示)。这将增加你的问题被回答和研究的机会。
欢迎使用stackoverflow,我希望这可以帮助您使代码正常工作!