【问题标题】:How do I format code to print with the same width every time?如何格式化代码以每次都以相同的宽度打印?
【发布时间】:2015-10-30 20:30:16
【问题描述】:

我正在制作一个游戏,我想知道是否有办法让 python 打印一个看起来像这样的地图,并且在打印最终结果时看起来不错?它应该是基于文本的游戏中的房间地图。我可以用命令格式化它吗?我只试过:

print("|------------------------------|")#print room map
print("|   [Chest]                 |")
print("|                            _ |")
print("|                       Door|")
print("|                             _|")
print("|   [Table]                 |")
print("|  (You)                     |")
print("|------------------------------|")#print room map

可以预见的是,上面的打印:

>>> print_room()
|------------------------------|
|   [Chest]                 |
|                            _ |
|                       Door|
|                             _|
|   [Table]                 |
|  (You)                     |
|------------------------------|

但是,在我的 IDE 中,每一行看起来都在同一个地方终止。如何编写代码以保证线条的宽度都相同?我希望它打印成这样:

|---------------------|
|                     |
|                     |
|                     |
|                     |
|---------------------|

【问题讨论】:

  • 你真正希望它是什么样子?
  • 您的命令提示符是否使用了奇怪的字体?通常,控制台将具有单宽字符的字体,在这种情况下,只需确保您打印的每一行具有相同数量的字符

标签: python string-formatting ascii-art


【解决方案1】:

您使用什么文本编辑器编写代码?我怀疑您使用的是 Word 或 Wordpad 之类的使用比例字体的工具。

我建议切换到专用的代码编辑器,例如 Notepad++UltraEdit,或者在最坏的情况下使用记事本。为文本文件制作并使用固定字体的编辑器,例如 Courier。除了许多其他好处之外,这将使您的地图线条更容易排列。

注意 Stack Overflow 如何使用固定字体?它表明您在每行上没有正确数量的空格。您的代码间距正确,看起来像您想要的那样:

print("|------------------------------|")#print room map
print("|   [Chest]                    |")
print("|                            _ |")
print("|                          Door|")
print("|                             _|")
print("|   [Table]                    |")
print("|  (You)                       |")
print("|------------------------------|")#print room map

【讨论】:

    【解决方案2】:

    您的基本问题是每行的长度必须相同。可能会干扰的事情:

    • 字体可能会显示不同宽度的不同字符。通常,“终端”窗口将使用固定宽度的字体:每个字符、空格等都具有完全相同的宽度。 “更高级别”的编辑器(例如Word)将使用可变宽度字体,因为它会更美观并且具有更广泛的审美用途。
    • 您对字符串所做的操作可能会改变实际长度。正如您在提供的示例中所看到的,不同数量的字符将以不同的宽度显示。因此,您的编程挑战是确保每行的宽度相同。

    你应该做些什么来确保你在构建你的字符串时变得聪明。

    让我们从基本的房间绳子、箱子和门开始:

    room = "|                     |"
    chest = "[Chest]"
    door = "Door"
    

    现在让我们编写一个在特定位置插入对象的函数:

    def insert_object(new_object: str, position: int) -> str:
        room_start = room[0:position]
        end_position = position + len(new_object)
        room_end = room[end_position:]
        new_room = room_start + new_object + room_end
        return new_room
    

    我们得到这样的结果:

    >>> room = "|                     |"
    >>> chest = "[Chest]"
    >>> door = "Door"
    >>> def insert_object(new_object: str, position: int) -> str:
    ...     room_start = room[0:position]  # Gets the part of the room before the 'object'
    ...     end_position = position + len(new_object)  # Gets the position after the object 'ends'
    ...     room_end = room[end_position:]  # Gets the part of the room after the object 'ends'
    ...     new_room = room_start + new_object + room_end  # Pieces these parts together, with the object in the middle
    ...     return new_room  # Returns the result
    ... 
    >>> x = insert_object(chest, 3)
    >>> print(x)
    '|  [Chest]            |'
    

    为房间的每一“行”插入一个项目,这样就可以保持宽度!

    room = "|                     |"
    chest = "[Chest]"
    door = "Door"
    wall = "---------------------"
    trap = "-"
    you = "You"
    table = "[Table]"
    
    def print_room():
        print(insert_object(wall,1))
        print(insert_object(chest,3))
        print(insert_object(trap,20))
        print(insert_object(door,18))
        print(insert_object(trap,21))
        print(insert_object(table,4))
        print(insert_object(you,3))
        print(insert_object(wall,1))
    

    导致:

    >>> print_room()
    |---------------------|
    |  [Chest]            |
    |                   - |
    |                 Door|
    |                    -|
    |   [Table]           |
    |  You                |
    |---------------------|
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用多行字符串,即。用三引号括起来的字符串可以超过一行。所以你可以这样做:

      map = """
                              |---------------------|
                              |                     |
                              |                     |
                              |                     |
                              |                     |
                              |---------------------|"""
      print(map)
      

      你是这个意思吗?

      编辑:在您的代码中,在添加“表格”等之后,您似乎还没有调整具有正确空格数的行。如果您有相同的代码,但添加了更多空格,您将能够让它以你的方式工作。您将其保存在脚本中然后运行它,对吗?不是在解释器中尝试吗?最后,专业提示:在终端中,每个字符占用相同宽度的空间,即。
      ..... 将与
      MMMMM 具有相同的宽度,因为在每种情况下它的字符数相同.

      【讨论】:

      • 我认为@John Kugelman 也有一个正确的想法:使用notepad++,在我看来它很棒,并且使用它可以让您看到需要在字符串中放入多少个字符。跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多