# ColorSpiralInput.py
import turtle
t = turtle.Pen()
turtle.bgcolor('black')
# Set up a list of any 8 valid Python color names
colors = ['red', 'yellow', 'blue', 'green', 'orange', 'purple', 'white', 'gray']
# Ask the user for the number of sides, between 1 and 8, with a default of 4
sides = int(turtle.numinput('Number of sides', 'How many sides do you want (1-8)?',
4, 1, 8))
# Draw a colorful spiral with the user-specified number of sides
for x in range(360):
t.pencolor(colors[x%sides]) # Only use the right number of colors
t.forward(x*3/sides+x) # Change the size to match number of sides
t.left(360/sides+1) # Turn 360 degrees / number of sides, plus 1
t.width(x*sides/200) # Make the pen larger as it goes outward
当我在sublime text 3中运行此代码时,程序运行后输出窗口立即关闭。我无法查看结果,然后手动关闭窗口
我已经检查了我的代码,但我没有解释。我写过其他脚本,但我没有遇到编辑器的相同行为
可能是我必须更改一些设置吗
对于在控制台中运行的通用Python turtle,执行的最终语句通常是
mainloop()
或其变体之一done()
或exitonclick()
。这将控制权移交给底层tkinter事件处理程序但是,当在IDE(如IDLE、PyCharm、SublimateText等)下运行时,规则会发生变化,并且不一致。有些人需要最终的
mainloop()
,有些人不想要,有些人把它变成了禁止操作。下面是我如何编写在控制台下运行的程序:如果您的IDE拒绝
mainloop()
、done()
和/或exitonclick()
,您可以始终使用“告诉用户在查看完您的输出后键入”,并在代码中放入最后的input()
调用相关问题 更多 >
编程相关推荐