Python代码不工作:不打印赢家
我想知道为什么我的代码一直在打印“tan”,我似乎无法让它打印出真正的赢家Python代码不工作:不打印赢家,python,turtle-graphics,Python,Turtle Graphics,我想知道为什么我的代码一直在打印“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,
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\u color[0]
有可能在每一场比赛结束时,获胜者实际上是“棕褐色的”
原因可能是尚未调用。因此,代码在每次运行时都将使用相同的种子进行初始化,从而导致每次生成的相同随机数序列具有相同的结果。您可以增加随机化,例如在每次调用之前初始化种子,或者仅在代码顶部初始化种子
添加此行:
random.seed() # initializes seed using current system time
代码中的任何地方都应该随机化结果
在宣读获奖者颜色时:我假设
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'
另外,看看如何以一种好的方式解决您的问题(stackoverflow文本编辑器还显示有关样式的工具提示)。这将增加你的问题被回答和研究的机会
欢迎使用stackoverflow,我希望这能帮助您使代码正常工作 我想我可能在这段代码中找到了错误 我发现在你的代码中,
turtle\u 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])
因此,正确的代码(完整)是:
告诉我是否还有任何错误(等) 这是你的实际缩进吗?如果是这样的话,
If(xcor>=finishline):
测试只在当前海龟的完成后进行,而不是每次当前海龟的测试一次。所以它只检查最后一只海龟。哪一个是棕褐色的。要解决这个问题,只需将函数的最后7行缩进到for
循环下即可。如果这是您的问题,并且您了解这一点的原因,我们可以将其作为简单类型关闭。如果你不理解缩进的重要性,我们可能会给你一个重复的问题,并给出一个像样的答案,但是你最好回到你所使用的任何书籍或教程中关于缩进的部分(或者可能是一个不同的部分,可能会有不同的解释)。如果这根本不是你的问题,请适当地编辑这个问题。正如abarnert所说:现在,你正在遍历你所有的海龟,并让它们前进。当前海龟
所取的最后一个值为棕褐色。在Python中,索引化非常重要,正如这里的代码所示,即使在循环完成之后,您仍然可以继续使用current\u-turtle
,因此:for循环之后的所有代码只针对current\u-turtle
(tan)的最后一个值执行。希望这有助于澄清。是的,这是我的实际缩进。谢谢你。这就是问题所在你关于random.seed()
的解释似乎不正确,如果不是与事实相反的话。创建一个导入random的程序,并像OP的代码一样执行打印(random.randint(0,10))
。您不会每次运行该程序都得到相同的号码。随机模块根据当前系统时间进行种子设定。但是,如果希望每次运行程序时随机序列都完全相同,可以使用random.seed()
实现这一点。概念上与您的解释相反。您关于winner\u color=current\u turtle.color()
的解释不正确。它返回一个元组,例如('black','tan')
,因此索引工作正常。但是,在这种情况下,使用pencolor()
或fillcolor()
获取感兴趣的颜色作为字符串,而不是同时请求并忽略一个,更有意义。
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()