【问题标题】:Python ASCII plotterPython ASCII 绘图仪
【发布时间】:2019-11-16 16:08:56
【问题描述】:

我正在用 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("\")

【问题讨论】:

    标签: python printing ascii


    【解决方案1】:

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

    print 函数可以在不总是转到下一行的情况下打印。例如,这将打印“Hello”。

    print("Hel", end="")
    print("lo")
    

    您可以将turtle 模块用于您想要的图形程序。你可能想看看module documentation

    您可以使用curses 模块在任何您想要的地方绘制字符。它比turtle 更难使用,但确实可以绘制字符。您可能想从How-To guide 开始。

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

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

    【讨论】:

      【解决方案2】:

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

      • "\033[F" – 将光标移动到上一行的开头
      • "\033[A" – 将光标上移一行

      示例(Python):

      print("\033[FMy text overwriting the previous line.")
      

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

      您可以查看Python module to enable ANSI colors for stdout on Windows? 看看它是否有用。

      colorama 模块似乎是跨平台的。

      你安装 colorama:

      pip install colorama
      

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

      【讨论】:

        【解决方案3】:

        在您的代码中,print("\") 不起作用,因为 '\' 是转义字符,因此您需要改为使用 print("\\")

        我认为跳入编写代码是一个错误——对我来说这是一个数据结构问题。而且数据结构越好,你需要编写的代码就越少。以下是我使用 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()
        

        输出

        【讨论】:

          猜你喜欢
          • 2012-07-27
          • 1970-01-01
          • 2013-12-16
          • 2022-10-05
          • 1970-01-01
          • 2023-02-19
          • 1970-01-01
          • 2016-11-27
          • 2013-01-22
          相关资源
          最近更新 更多