我正在为学校工作。它很简单,涉及文件io和乌龟图形,我无法弄清楚为什么最后一行显示与其他部分断开连接,如下所示:picture of output
import turtle
def gfxFileIO():
t1 = turtle.Turtle()
window = turtle.Screen()
f=open("file.txt","r")
count = 1
t1.penup()
t1.rt(270)
t1.fd(250)
t1.rt(270)
t1.fd(300)
t1.rt(270)
t1.write("Your file: ", font=("Arial", 16, "normal"))
t1.fd(45)
for line in f:
t1.write(str(count) + ". " + str(line), font=("Arial", 16, "normal"))
t1.fd(16)
count += 1
window.exitonclick()
答案 0 :(得分:0)
文本文件有额外的换行符。谢谢用户DYZ
答案 1 :(得分:0)
发布后,此代码无法运行。 window
是gfxFileIO()
的局部变量,但全局使用。永远不会调用一个函数gfxFileIO()
。既然已经解决了数据输入问题,让我们重新考虑示例代码:
from turtle import Turtle, Screen
FONT_SIZE = 16
FONT = ("Arial", FONT_SIZE, "normal")
def gfxFileIO(turtle, file_name):
file = open(file_name)
turtle.penup()
turtle.rt(270)
turtle.fd(250)
turtle.rt(270)
turtle.fd(300)
turtle.rt(270)
turtle.write("Your file: ", font=FONT)
turtle.fd(FONT_SIZE * 3) # double space
count = 1
for line in file:
turtle.write("{}. {}".format(count, line), font=FONT)
turtle.fd(FONT_SIZE)
count += 1
yertle = Turtle()
gfxFileIO(yertle, "file.txt")
screen = Screen()
screen.exitonclick()