我将如何编写一个基于高度和宽度制作图案(如下图所示)的程序?[已关闭]

How would I write a program that makes a pattern (like the one below) based on height and width? [closed]

提问人:Kaia 提问时间:9/9/2023 最后编辑:Kaia 更新时间:9/19/2023 访问量:41

问:


想改进这个问题吗?通过编辑这篇文章来更新问题,使其仅专注于一个问题。

3个月前关闭。

程序应根据用户输入的名称和图块高度设计“数字磁贴”。磁贴宽度是用户输入的名称长度的 3 倍。

老实说,我不知道我会用什么样的公式来获得侧面有所有破折号的菱形图案。谁能帮我?

输出示例如下:

Name: Tommy
Tile height: 5

------+|+------
---+|++|++|+---
-----TOMMY-----
---+|++|++|+---
------+|+------


Name: Julia
Tile height: 13

------------------+|+------------------
---------------+|++|++|+---------------
------------+|++|++|++|++|+------------
---------+|++|++|++|++|++|++|+---------
------+|++|++|++|++|++|++|++|++|+------
---+|++|++|++|++|++|++|++|++|++|++|+---
-----------------JULIA-----------------
---+|++|++|++|++|++|++|++|++|++|++|+---
------+|++|++|++|++|++|++|++|++|+------
---------+|++|++|++|++|++|++|+---------
------------+|++|++|++|++|+------------
---------------+|++|++|+---------------
------------------+|+------------------

我通常知道如何编写程序并使其工作,我只是不知道如何使用某种增量代码或其他东西来获取磁贴图案。我也不知道如何让它注册它已经到达中线并应该打印名称。

评论

0赞 mkrieger1 9/9/2023
你能展示你能够编写的最接近你想要实现的程序吗?
0赞 Swifty 9/9/2023
两件事:如果名称的长度是偶数,那么模式应该是什么?你的第二个例子不是与规则“width = 3 * name_length”相矛盾吗?

答:

0赞 mr_H 9/9/2023 #1

我假设图块是准确的,并且您的描述不完整/不准确,因为第二个图块肯定比“用户输入的名称长度的 3 倍”长。

我看到的规则实际上如下:

  1. 不包含名称的行由两种类型的三个字符长部分组成:“---”和“+|+”。
  2. 顶行中间包含一个“+|+”,接下来的每一行都将用相同的字符串替换两个“---”部分(每个方向一个)。
  3. 名称前面的行仍将有两个“---”部分(每侧一个)。
  4. 行长度等于 3 * 图块高度。
  5. 我隐含地假设只有奇数被接受为高度,因为你的例子中看到的结构。
import math

height = 9
length = height * 3

# calculating the number of tile rows above and below the name row
outside_rows = math.floor(height/2)

# building rows above the name row
for n in range(0,outside_rows):
    tile_sections = 2 * n + 1
    background_length = int((length - tile_sections * 3) / 2)
    print("-" * background_length, "+|+" * tile_sections, "-" * background_length, sep='')

# you can print your named row here
print("")

# building rows below the name row
for n in range(outside_rows-1, -1, -1):
    tile_sections = 2 * n + 1
    background_length = int((length - tile_sections * 3) / 2)
    print("-" * background_length, "+|+" * tile_sections, "-" * background_length, sep='')

输出:

------------+|+------------
---------+|++|++|+---------
------+|++|++|++|++|+------
---+|++|++|++|++|++|++|+---

---+|++|++|++|++|++|++|+---
------+|++|++|++|++|+------
---------+|++|++|+---------
------------+|+------------

评论

1赞 Swifty 9/9/2023
足够好,但是在 --- 和 +|+ 之间有一个(我假设)不需要的空间。
0赞 mr_H 9/19/2023
事实上。现已修复。感谢您的注意。