简体   繁体   English

我想在不同的行上打印每一行

[英]I want to print each line on a different row

This is my code: (I am trying to print a text file that contains song lyrics as an image. Using python turtle, drawing squares to represent pixels; printing this as the format of the lyrics in the textfile to make a picture.)这是我的代码:(我试图将包含歌词的文本文件打印为图像。使用python乌龟,绘制正方形来表示像素;将其打印为文本文件中歌词的格式以制作图片。)

import turtle
t=turtle.Turtle()
canvas=turtle.Screen()
canvas.setup(width=1280, height=720)
t.speed(0)

#The drawing starts in the upper left corner of the canvas.
t.penup()
t.goto(-600,325)
t.pendown()

def square(color):
    
    t.fillcolor(color)
    t.begin_fill()
    for i in range(4):
        t.forward(10)
        t.right(90)
    t.end_fill()


def paint_line (file_text):
    """
The paint line() function shall take one parameter as input, a string representing
a line from a text file, and draw a row of colorful squares corresponding to the
characters in the line.
After drawing the row, the turtle shall go to the beginning of the next row to be
ready to paint the next line, if any.
    """

    count=0
    for ch in file_text:
        count+=1
        if (ord(ch)<70):
            square('black')
        elif (ord(ch)>=70 and ord(ch)<100):
            square('pink')
        elif (ord(ch)>=100 and ord(ch)<110):
            square('light blue')
        elif (ord(ch)>=110 and ord(ch)<122):
            square('yellow')
        elif (ord(ch)>122):
            square('green')

        #to print each line of text on a different row
        t.penup()
        t.left(180)
        t.pendown()
    

#do not use readlines()
def picture(file_name):
    '''
The picture() function takes one parameter as input, a file name, opens the file,
and draws the picture by calling the paint line() function for each line of text in
the file. 
    '''
    file_data=open(file_name+".txt","r")
    file_text=file_data.readline()
    count=1
    while file_text:
        print("Line {}: {}".format(count, file_text.strip()))

        file_text=file_data.readline()
        paint_line(file_text)
        count += 1
        
    file_data.close() 

    

def main():
    '''
The main() function prompts the user for a file name and calls picture() to do
the work.
    '''      
    file_name=input("Enter the file Name")
    picture(file_name)

main()

This is what the text file contains:这是文本文件包含的内容:

Strawberries, cherries and an angel kissing spring草莓、樱桃和天使亲吻春天
My summer wine is really made from all these things我的夏酒真的是用这些东西制成的
I walked in town on silver spurs that jingled to我踩着叮叮当当的银色马刺在镇上行走
A song that I had only sang to just a few一首我只唱给少数人听的歌
She saw my silver spurs and said let's pass some time她看到了我的银色马刺,说让我们过去一段时间
And I will give to you, summer wine我会给你,夏酒
Oh.哦。 oh, oh, summer wine哦,哦,夏酒
Strawberries, cherries and an angel kissing spring草莓、樱桃和天使亲吻春天
My summer wine is really made from all these things我的夏酒真的是用这些东西制成的
Take off your silver spurs and help me pass the time脱下你的银色马刺,帮我打发时间
And I will give to you, summer wine我会给你,夏酒
Oh, summer wine哦,夏酒
My eyes grew heavy and my lips they could not speak我的眼睛变得沉重,我的嘴唇无法说话
I tried to get up but I couldn't find my feet我试图站起来,但我找不到我的脚
She reassured me with the unfamiliar line她用陌生的台词让我放心
And then she gave to me, more summer wine然后她给了我更多的夏酒
Woh, woh, oh, summer wine呜呜呜夏天酒
Strawberries, cherries and an angel kissing spring草莓、樱桃和天使亲吻春天
My summer wine is really made from all these things我的夏酒真的是用这些东西制成的
Take off your silver spurs and help me pass the time脱下你的银色马刺,帮我打发时间
And I will give to you, summer wine我会给你,夏酒
Mm, summer wine嗯,夏酒
When I woke up, the sun was shining in my eyes当我醒来时,阳光照在我的眼睛里
My silver spurs were gone, my head felt twice its size我的银色马刺不见了,我的头感觉是它的两倍大
She took my silver spurs, a dollar and a dime她拿走了我的银马刺,一美元一角钱
And left me craving for, more summer wine让我渴望更多的夏日美酒
Oh, oh, summer wine哦,哦,夏酒
Strawberries, cherries and an angel kissing spring草莓、樱桃和天使亲吻春天
My summer wine is really made from all these things我的夏酒真的是用这些东西制成的
Take off those silver spurs, help me pass the time脱下那些银色的马刺,帮我打发时间
And I will give to you my summer wine我会把我的夏酒给你
Oh, oh, summer wine哦,哦,夏酒

This is the output I get:这是我得到的输出:

This is the output I expected:这是我期望的输出:

You have forgotten to reset your pen's position correctly.您忘记正确重置笔的位置。

import turtle
t = turtle.Turtle()
canvas = turtle.Screen()
canvas.setup(width=1280, height=720)
t.speed(0)

#The drawing starts in the upper left corner of the canvas.
t.penup()
t.goto(-600, 325)
t.pendown()


def square(color):

t.fillcolor(color)
t.begin_fill()
for i in range(4):
t.forward(10)
t.right(90)
t.end_fill()
t.forward(10)


def paint_line(file_text):
"""
The paint line() function shall take one parameter as input, a string representing
a line from a text file, and draw a row of colorful squares corresponding to the
characters in the line.
After drawing the row, the turtle shall go to the beginning of the next row to be
ready to paint the next line, if any.
"""

count = 0
for ch in file_text:
count += 1
if (ord(ch) < 70):
square('black')
elif (ord(ch) >= 70 and ord(ch) < 100):
square('pink')
elif (ord(ch) >= 100 and ord(ch) < 110):
square('light blue')
elif (ord(ch) >= 110 and ord(ch) < 122):
square('yellow')
elif (ord(ch) > 122):
square('green')

#to print each line of text on a different row
t.penup()
# t.right(20)
t.pendown()


#do not use readlines()
def picture(file_name):
'''
The picture() function takes one parameter as input, a file name, opens the file,
and draws the picture by calling the paint line() function for each line of text in
the file.
'''
file_data = open(file_name + ".txt", "r")
file_text = file_data.readline()
count = 1
while file_text:
print("Line {}: {}".format(count, file_text.strip()))
file_text = file_data.readline()
paint_line(file_text)
t.penup()
t.goto(-600, 325 - count * 10)
t.pendown()
count += 1
file_data.close()


def main():
'''
The main() function prompts the user for a file name and calls picture() to do
the work.
'''
file_name = 'test'  #input("Enter the file Name")
picture(file_name)


main()

Along with turning your turtle completely around instead of moving it forward, you've also skip over character 122 ('z') in your logic.除了将您的海龟完全转动而不是向前移动之外,您还跳过了逻辑中的字符 122 ('z')。

To both simplify the code, and speed up the program, I would approach this as a stamping problem rather than a drawing one:为了简化代码并加速程序,我会将其视为冲压问题而不是绘图问题:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 1280, 720

TILE_SIZE = 10
CURSOR_SIZE = 20

def square(color):

    turtle.fillcolor(color)
    turtle.stamp()

def paint_line(text):
    '''
    The paint line() function shall take one parameter as input, a string representing
    a line from a text file, and draw a row of colorful squares corresponding to the
    characters in the line.
    After drawing the row, the turtle shall go to the beginning of the next row to be
    ready to paint the next line, if any.
    '''

    for character in text:
        code = ord(character)

        if code < 70:
            square('black')
        elif 70 <= code < 100:
            square('pink')
        elif 100 <= code < 110:
            square('light blue')
        elif 110 <= code < 122:
            square('yellow')
        elif code >= 122:
            square('green')

        # to print each line of text on a different row
        turtle.forward(10)

#do not use readlines()
def picture(file_name):
    '''
    The picture() function takes one parameter as input, a file name, opens the file,
    and draws the picture by calling the paint line() function for each line of text in
    the file.
    '''

    with open(file_name + ".txt") as file_data:
        for line, text in enumerate(file_data, start=1):
            print("Line {}: {}".format(line, text.strip()))

            paint_line(text)

            turtle.goto(-WIDTH/2 + TILE_SIZE, turtle.ycor() - TILE_SIZE)

def main():
    '''
    The main() function prompts the user for a file name and calls picture() to do
    the work.
    '''

    file_name = input("Enter the file name: ")
    picture(file_name)

screen = Screen()
screen.setup(WIDTH, HEIGHT)

turtle = Turtle()
turtle.shape('square')
turtle.shapesize(TILE_SIZE / CURSOR_SIZE)
turtle.speed('fastest')
turtle.penup()

# The drawing starts near the upper left corner of the canvas.
turtle.goto(-WIDTH/2 + TILE_SIZE, HEIGHT/2 - TILE_SIZE)

main()

screen.exitonclick()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在单独的行上打印每个数字的平方 - I want print the square of each number on a separate line Matplotlib:希望每行有不同的图 - Matplotlib: want different plot for each line 我想使用python提取并打印每个块的第一行中包含10917、11396和1116920的块 - Using python, I want to extract and print blocks that contain 10917, 11396 and 1116920 in first line of each block 迭代 dataframe 并为每一行分配值 - 我得到相同的值,但我想要不同的值 - Iterate dataframe and assign value to each row- I get the same value while I want different ones 我想在比赛前后打印该行 - I want to print the line before and after match 在 Python 中,如何逐行读取文本文档并在每行末尾打印一行中相同字符的数量? - In Python, how can I read a text document line-by-line and print the number of same characters in a row at the end of each line? 我想将每一行保存为列表 - I want to save each row as a list 我想在每行中使用 2 个逗号 - I want to get between 2 commas in each line 我想在每个循环中选择不同的元素 - I want to choose different element in each loop 我有一个csv文件,我想将csv文件的每一行提取到不同的csv文件中。 我怎样才能做到这一点? - I have a csv file and i want to extract each row of csv file into different csv file . how can i do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM