我已经实现了一个小程序,它的一个功能是打开和读取带有绘图说明的文本文件,而第二个功能则针对绘图。第二个已经被使用过,所以应该是正确的。问题是,在测试程序时,海龟窗口打开,但实际上什么也没发生。我认为问题出在read_file函数上。我想我以错误的方式实现了一些东西。试图从文本文档中制作二维列表,但未能成功。因此,程序应该使用 read_file (因此它应该包含“打开”方法)函数打开一个文件,并根据提取的指令绘制几个螺旋。我的海龟窗口只有一个白色的屏幕。你猜,绘图过程从未开始
我做错了什么? 谢谢!
from turtle import *
def draw(col, arc_number, basic_radius, rad_growth, pen_weight):
color(col)
pensize(pen_weight)
for i in range(arc_number):
circle(basic_radius, 90)
basic_radius += rad_growth
def read_file(name_of_file):
with open(name_of_file) as filename:
filename.read().splitlines()
for spiral in filename:
for col, arc_number, basic_radius, rad_growth, pen_weight in spiral:
draw(col, arc_number, basic_radius, rad_growth, pen_weight)
read_file("instruction.txt")
done()
名为“instruction.txt”的文本文件包含颜色、弧数、半径、半径增长、笔粗细:
black,35,9,6,1
red,15,25,4,2
blue,12,-19,-4,3
(所以,txt 文件中有 3 行(每个螺旋 1 行)
在
read_file
中,您读入文件的内容,并将它们分成几行,但然后不对它们执行任何操作。现在文件中没有任何内容可供读取,因此后面的循环永远不会运行。