簡體   English   中英

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

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

這是我的代碼:(我試圖將包含歌詞的文本文件打印為圖像。使用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()

這是文本文件包含的內容:

草莓、櫻桃和天使親吻春天
我的夏酒真的是用這些東西制成的
我踩着叮叮當當的銀色馬刺在鎮上行走
一首我只唱給少數人聽的歌
她看到了我的銀色馬刺,說讓我們過去一段時間
我會給你,夏酒
哦。 哦,哦,夏酒
草莓、櫻桃和天使親吻春天
我的夏酒真的是用這些東西制成的
脫下你的銀色馬刺,幫我打發時間
我會給你,夏酒
哦,夏酒
我的眼睛變得沉重,我的嘴唇無法說話
我試圖站起來,但我找不到我的腳
她用陌生的台詞讓我放心
然后她給了我更多的夏酒
嗚嗚嗚夏天酒
草莓、櫻桃和天使親吻春天
我的夏酒真的是用這些東西制成的
脫下你的銀色馬刺,幫我打發時間
我會給你,夏酒
嗯,夏酒
當我醒來時,陽光照在我的眼睛里
我的銀色馬刺不見了,我的頭感覺是它的兩倍大
她拿走了我的銀馬刺,一美元一角錢
讓我渴望更多的夏日美酒
哦,哦,夏酒
草莓、櫻桃和天使親吻春天
我的夏酒真的是用這些東西制成的
脫下那些銀色的馬刺,幫我打發時間
我會把我的夏酒給你
哦,哦,夏酒

這是我得到的輸出:

這是我期望的輸出:

您忘記正確重置筆的位置。

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()

除了將您的海龜完全轉動而不是向前移動之外,您還跳過了邏輯中的字符 122 ('z')。

為了簡化代碼並加速程序,我會將其視為沖壓問題而不是繪圖問題:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM