简体   繁体   English

用Python制作钻石ASCII艺术品

[英]Making diamond ASCII art with Python

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)

When char length is odd 当char长度为奇数时

When char length is even 当字符长度均匀时

This is python. 这是python。 There are a multitude of useful string functions you could use to create inventive ASCII art in a handful lines of code. 您可以使用许多有用的字符串函数在少量代码行中创建创造性的ASCII艺术。

Some of the most important ones would be str.join , str.Xjust . 一些最重要的将是str.joinstr.Xjust We'll also make use of chr and ord to iterate over character ranges. 我们还将使用chrord迭代字符范围。

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

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. 两次循环都在每次迭代时调用pad

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' : 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' : 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's answer explores an alternative approach which involves building the first half of the diamond and reversing it to obtain the second half. Seth Difley的回答探讨了一种替代方法,包括建造钻石的前半部分并将其反转以获得下半部分。 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. 这也导致相同的输出,并且更快。

Strategy: since the top half of the diamond is rendered correctly by the existing program, generate the top half and then generate the bottom half by reversing the lines from the top half. 策略:由于钻石的上半部分由现有程序正确渲染,因此生成上半部分,然后通过反转上半部分的线条生成下半部分。 build_diamond returns a list containing the strings for the top half. build_diamond返回一个包含上半部分字符串的列表。 print('\\n'.join(string_list)) prints the top half. print('\\n'.join(string_list))打印上半部分。 bottom_of_diamond_string_list = list(reversed(string_list))[1:] reverses the strings from the top half and removes the middle string with [1:] to get the strings for the bottom half. bottom_of_diamond_string_list = list(reversed(string_list))[1:]从上半部分反转字符串并用[1:]删除中间字符串以获取下半部分的字符串。 print('\\n'.join(bottom_of_diamond_string_list)) prints the bottom half. print('\\n'.join(bottom_of_diamond_string_list))打印下半部分。 Tested and works for 5 and 6 (even and odd) chars length. 测试并适用于5和6(偶数和奇数) chars长度。 A lot more code clean up can be done, if desired. 如果需要,可以进行更多代码清理。

chars = 'ABCDEF'
length = len(chars)

def build_diamond(length):
    dots = (length*2 - 1)*2 - 1
    string = ''
    string_list = []
    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, '.')
        string_list.append(string1)
    return string_list

if __name__ == '__main__':
    string_list = build_diamond(length)
    print('\n'.join(string_list))
    bottom_of_diamond_string_list = list(reversed(string_list))[1:]
    print('\n'.join(bottom_of_diamond_string_list))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM