使用Python制作钻石ASCII艺术 [英] Making diamond ASCII art with Python

查看:259
本文介绍了使用Python制作钻石ASCII艺术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制造这颗钻石时遇到了麻烦。每当我使字符等于偶数长度时,结果就很好。但是,当它很奇怪时,只会将钻石的底部弄乱。我已经工作了几个小时,几乎完成了。预先感谢您的帮助。

I'm having trouble making this diamond. Whenever I make chars equal to an even length, it turns out fine. However, when it is odd, only the bottom portion of the diamond gets messed up. I've been working hours on this and am almost done. Thanks in advance for the help.

chars = 'ABCDEF'
length = len(chars)
string = ''
dots = (length*2 - 1)*2 - 1
for i in range(length):
    string1 = ''
    string += chars[i]
    length1 = len(string)
    for j in range(0, length1):
        if j % 2 != 0:
            string1 += chars[length -1 - j].center(3, '.')
        else:
            string1 += chars[length - 1 - j]
    for k in range(i - 1, -1, -1):
        if k % 2 != 0:
            string1 += chars[length - 1 - k].center(3, '.')
        else:
            string1 += chars[length - 1 - k]
    string1 = string1.center(dots, '.')
    print(string1)

string=''
for i in range(length - 1):
    string1 = ''
    string += chars[i]
    length1 = len(string)
    for j in range(length - 1 - i):
        if j % 2 != 0:
            string1 += chars[length - 1 - j]
        else:
            string1 += chars[length -1 - j].center(3, '.')
    for k in range(i + 2, length):
        if k % 2 != 0:
            string1 += chars[k].center(3, '.')
        else:
            string1 += chars[k]
    string1 = string1.center(dots, '.')     
    print(string1)

当字符长度为奇数时

当字符长度为偶数时

推荐答案

这是python。您可以使用许多有用的字符串函数在少量代码中创建创造性的ASCII艺术。

This is python. There are a multitude of useful string functions you could use to create inventive ASCII art in a handful lines of code.

一些最重要的是 str.join str.Xjust 。我们还将利用 chr ord 遍历字符范围。

Some of the most important ones would be str.join, str.Xjust. We'll also make use of chr and ord to iterate over character ranges.

首先,定义一个将处理填充的函数。

First, define a function that'll handle padding.

def pad(c1, c2, sep='.', field_width=10):
    out = sep.join(chr(x) for x in range(c2, c1, -1)).rjust(field_width, sep) # build the first part 
    return sep.join([out, chr(c1), out[::-1]])

第一行代码将构建菱形行的前半部分。第二行将上半部分与中心字母连接在一起,并将上半部分的反向版本连接起来。

The first line of code will build the first half of the diamond line. The second line joins the first half with the centre letter, and the reversed version of the first half.

下一步,确定范围-钻石的尺寸

Next, determine the range - how big your diamond is going to be.

start = 'A'
end = ...
field_width = (ord(end) - ord('A')) * 2 - 1

现在,您需要两个单独的循环-一个用于上面的菱形,另一个用于下面的菱形。这两个循环在每次迭代时都调用 pad

Now, you'll need two separate loops - one for the upper diamond and the other for the lower one. Both loops call pad at each iteration.

for e in range(ord(end), ord(start), -1):
    print(pad(e, ord(end), '.', field_width))

for e in range(ord(start), ord(end) + 1):
    print(pad(e, ord(end), '.', field_width))






end ='E'

........E........
......E.D.E......
....E.D.C.D.E....
..E.D.C.B.C.D.E..
E.D.C.B.A.B.C.D.E
..E.D.C.B.C.D.E..
....E.D.C.D.E....
......E.D.E......
........E........

end ='F'

..........F..........
........F.E.F........
......F.E.D.E.F......
....F.E.D.C.D.E.F....
..F.E.D.C.B.C.D.E.F..
F.E.D.C.B.A.B.C.D.E.F
..F.E.D.C.B.C.D.E.F..
....F.E.D.C.D.E.F....
......F.E.D.E.F......
........F.E.F........
..........F..........






Seth Difley的答案探索了另一种方法,该方法包括建造钻石的上半部分并将其反转以获得下半场。确实,这种解决方案也可以采用这种方法,类似于:


Seth Difley's answer explores an alternative approach which involves building the first half of the diamond and reversing it to obtain the second half. Indeed, this approach can also be adopted to this solution, something along the lines of:

lines = []
for e in range(ord(end), ord(start) - 1, -1):
    lines.append(pad(e, ord(end), '.', field_width))

for x in lines + lines[-2::-1]:
    print(x)

这也会产生相同的输出,并且速度更快。

Which also results in the same output, and is faster.

这篇关于使用Python制作钻石ASCII艺术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆