求解这个Python题

请给我完整的正确代码过程截图和实验结果截图。
请一定按照图片上面的做题步骤来进行,最终要得到一个田字格的图片。

img


import random
a=['0','1','2','3','4','5','6','7','8','9','10','_']
b=[]
for i in range(26):
    i+=65
    r = chr(i)
    a.append(r)
for i in range(26):
    i+=97
    r = chr(i)
    a.append(r)
for i in range(7):
    s = random.randint(0, 63)
    b.append(a[s])
    print(a[s],end=' ')
print()
for i in range(5):
    if i!=2:
        for j in range(7):
            if j==0 or j ==6or j==3:
                s = random.randint(0, 63)
                b.append(a[s])
                print(a[s], end=' ')
            else:
                print(" ", end=' ')
        print()
    else:
        for i in range(7):
            s = random.randint(0, 63)
            b.append(a[s])
            print(a[s], end=' ')
        print()
for i in range(7):
    s = random.randint(0, 63)
    b.append(a[s])
    print(a[s], end=' ')
print()
dic={}
for i in b:
    dic[i]=b.count(i)
print(dic)

你题目的解答代码如下:

import random
lst=list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
 
width = 10
s = "".join([random.choice(lst) for i in range(width*4+3)])+"\n"
for i in range(2):
    for j in range(width):
        s += random.choice(lst)+ " "*(width*2) +random.choice(lst)+ " "*(width*2) +random.choice(lst)+"\n"
    s += "".join([random.choice(lst) for i in range(width*4+3)])+"\n"
print(s)
 
dic = {}
for v in s:
    if v in lst:
        dic[v] = dic.get(v,0)+1
for k,v in dic.items():
    print(f'{k}:{v}')

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

做了详细注释,希采纳谢谢!

from random import choices
from string import ascii_letters, digits

n = int(input()) #输入田字的边长
ASCII = ascii_letters+digits+'_'
lines = [choices(ASCII, k=n) for _ in range(n)]
lst = [0,len(lines)//2,len(lines)-1] #田字三条横线的行号
for i in range(len(lines)):
    if i not in lst:
        lines[i]=lines[i][:3]
        if len(lines)%2==0:
            lines[i][-1]=' '+lines[i][-1]
        #边长为偶数时,解决田字中的十不可能居中的显示问题

for i,n in enumerate(lines):
    t = '' if i in lst else ' '*((len(lines)-3)//2)  #输出时空格数
    print(t.join(n))

Letters = ''.join(sum(lines,[])).replace(' ','')  #把所有字符拼成一个字串
counter = {a:Letters.count(a) for a in Letters} #统计各字符个数

with open('dictory.txt','w') as file:  # 遍历字典写入文件
    for k,v in counter.items():
        file.write(f'{k}:{v}\n')
import string
import random
from collections import Counter

dirname=r'C:\Users\Administrator\Desktop'

s = string.ascii_letters + string.digits + '_'
length = int(input(">>>"))

alpha = []
for i in range(1, length + 1):
    if i in [1, (length+1)//2, length]:
        s1 = random.sample(s, k = length)
        alpha.extend(s1)        
        print(''.join(s1))
    else:
        s2 = random.sample(s, k = 3)
        alpha.extend(s2)
        s2 = (' ' * ((length - 3) // 2)).join(s2)
        print(s2)
with open(dirname + "/dict.txt", 'w', encoding = 'utf-8') as f:
        for k, v in Counter(alpha).items():
            print("{}:{}".format(k, v), file = f)