# -*- coding: utf-8 -*- """ Created on Tue Apr 18 19:28:37 2023 @author: ymp """ 杨梦萍的作业一

杨梦萍的作业一

代码


# replace with your code here
# 读取文件
with open('a.txt', encoding='utf-8') as file:
    content = file.readlines()
    # 读取txt倒数第二行
    data_line = content[-2]
    data_char = ''
    # 字符在字符串中的序号
    char_num = -1
    count = 0
    # 读取到空格截至
    while data_char != ' ':
        data_char = data_line[char_num]
        # 字符属于字母
        if ('a' <= data_char <= 'z') or ('A' <= data_char <= 'Z'):
            # 属于字母计数加一
            count = count + 1
        else:
            pass
        char_num = char_num - 1
print(count)

结果

结果截图

解释

如果所写代码复杂,且没有良好的注释,那么请在这里补充解释。