Python ASCII绘图仪

Python ASCII绘图仪,python,printing,ascii,Python,Printing,Ascii,我正在用Python做一个ascii问题。目标是使用特定值创建图像以创建特定图像: 值0创建一个“-”并将光标向右移动,以获得下一个变量。1创建“/”并将光标向上移动一行并向右移动。2创建一个“|”,并将光标向上移动一行到同一位置。3创建一个“\”并将光标向上移动一行,再向左移动一个空格。4创建一个“-”并向左移动光标。5创建“/”并将光标向下移动一行并向左移动。6创建一个“|”并将光标向下移动一行。7创建一个“\”并将光标向下移动一行并向右移动 然而,我不知道如何打印这使它发生。我的代码: f

我正在用Python做一个ascii问题。目标是使用特定值创建图像以创建特定图像:

值0创建一个“-”并将光标向右移动,以获得下一个变量。1创建“/”并将光标向上移动一行并向右移动。2创建一个“|”,并将光标向上移动一行到同一位置。3创建一个“\”并将光标向上移动一行,再向左移动一个空格。4创建一个“-”并向左移动光标。5创建“/”并将光标向下移动一行并向左移动。6创建一个“|”并将光标向下移动一行。7创建一个“\”并将光标向下移动一行并向右移动

然而,我不知道如何打印这使它发生。我的代码:

file_object = open("input.txt","w")

file_object.write("770000334444")
file_object.write("2255500004666")
file_object.write("002444")

file_object.close()

file_object = open("input.txt","r")


while True:
    c = file_object.read(1)
    if c == "0":
        print("-")
    if c == "1":
        print("/")
    if c == "2":
        print("|")
    if c == "3":
        print("\")
    if c == "4":
        print("-")
    if c == "5":
        print("/")
    if c == "6":
        print("|")
    if c == "7":
        print("\")

恭喜你解决了一个有趣的问题。因为你有一个开放式的问题,我提供一些一般信息,希望能对你有所帮助

print
功能可以打印,而不必总是转到下一行。例如,这将打印“Hello”

您可以使用
turtle
模块来编写您想要的图形程序。你可能想看看

您可以使用
诅咒
模块在任何需要的地方绘制字符。它比海龟更难使用,但它确实能画字符。你可能想从这个开始

您可以创建自己的字符行列表,填写并打印它们。这是一种更加困难的方法,它不使用任何库


玩得开心!继续编码!记笔记。

祝贺你解决了一个有趣的问题。因为你有一个开放式的问题,我提供一些一般信息,希望能对你有所帮助

print
功能可以打印,而不必总是转到下一行。例如,这将打印“Hello”

您可以使用
turtle
模块来编写您想要的图形程序。你可能想看看

您可以使用
诅咒
模块在任何需要的地方绘制字符。它比海龟更难使用,但它确实能画字符。你可能想从这个开始

您可以创建自己的字符行列表,填写并打印它们。这是一种更加困难的方法,它不使用任何库


玩得开心!继续编码!记笔记。

如果您在linux中运行它,大多数终端都无法理解。此用例的相关代码:

  • “\033[F”
    –将光标移动到上一行的开头
  • “\033[A”
    –将光标上移一行
示例(Python):

但是如果您使用的是windows,则需要使用启用ansi转义码的模块

你可以看看它是否有用

该模块似乎是跨平台的

您要安装colorama:

pip install colorama

然后尝试在代码中使用ANSI转义码。:)

如果您在linux中运行它,大多数终端都无法理解。此用例的相关代码:

  • “\033[F”
    –将光标移动到上一行的开头
  • “\033[A”
    –将光标上移一行
示例(Python):

但是如果您使用的是windows,则需要使用启用ansi转义码的模块

你可以看看它是否有用

该模块似乎是跨平台的

您要安装colorama:

pip install colorama

然后尝试在代码中使用ANSI转义码。

在代码中,
打印(“\”
将不起作用,因为
“\”
是转义字符,因此您需要改为执行
打印(“\”

我认为直接编写代码是一个错误——对我来说,这是一个数据结构问题。数据结构越好,需要编写的代码就越少。下面是我使用Python turtle解决这个问题的方法:

from turtle import Screen, Turtle

FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'normal')

OPERATORS = [
    ["-",  ( 1,  0)],  # 0 draws "-" and moves cursor to the right
    ["/",  ( 1,  1)],  # 1 draws "/" and moves cursor up one row and to the right
    ["|",  ( 0,  1)],  # 2 draws "|" and moves cursor up one row in the same position
    ["\\", (-1,  1)],  # 3 draws "\" and moves cursor up one row and over to the left
    ["-",  (-1,  0)],  # 4 draws "-" and moves cursor left
    ["/",  (-1, -1)],  # 5 draws "/" and moves cursor down one row and to the left
    ["|",  ( 0, -1)],  # 6 draws "|" and moves cursor down one row
    ["\\", ( 1, -1)],  # 7 draws "\" and moves cursor down one row and to the right
]

def display(turtle, string):
    for digit in string:
        character, (dx, dy) = OPERATORS[int(digit)]
        x, y = turtle.position()
        turtle.write(character, align='center', font=FONT)
        turtle.goto(x + dx * FONT_SIZE, y + dy * FONT_SIZE)

if __name__ == "__main__":
    turtle = Turtle()
    turtle.hideturtle()
    turtle.penup()

    display(turtle, "2255500004666")

    screen = Screen()
    screen.exitonclick()
输出


在代码中,
打印(\”
不起作用,因为
“\”
是转义字符,因此您需要执行
打印(\”

我认为直接编写代码是一个错误——对我来说,这是一个数据结构问题。数据结构越好,需要编写的代码就越少。下面是我使用Python turtle解决这个问题的方法:

from turtle import Screen, Turtle

FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'normal')

OPERATORS = [
    ["-",  ( 1,  0)],  # 0 draws "-" and moves cursor to the right
    ["/",  ( 1,  1)],  # 1 draws "/" and moves cursor up one row and to the right
    ["|",  ( 0,  1)],  # 2 draws "|" and moves cursor up one row in the same position
    ["\\", (-1,  1)],  # 3 draws "\" and moves cursor up one row and over to the left
    ["-",  (-1,  0)],  # 4 draws "-" and moves cursor left
    ["/",  (-1, -1)],  # 5 draws "/" and moves cursor down one row and to the left
    ["|",  ( 0, -1)],  # 6 draws "|" and moves cursor down one row
    ["\\", ( 1, -1)],  # 7 draws "\" and moves cursor down one row and to the right
]

def display(turtle, string):
    for digit in string:
        character, (dx, dy) = OPERATORS[int(digit)]
        x, y = turtle.position()
        turtle.write(character, align='center', font=FONT)
        turtle.goto(x + dx * FONT_SIZE, y + dy * FONT_SIZE)

if __name__ == "__main__":
    turtle = Turtle()
    turtle.hideturtle()
    turtle.penup()

    display(turtle, "2255500004666")

    screen = Screen()
    screen.exitonclick()
输出