justdbmore 发表于 2020-3-13 22:06:10

035 课后习题关于eaygui几题中的部分代码不太理解

1. 实现一个用于登记用户账号信息的界面(如果是带 * 号的必填项,要求一定要有输入并且不能是空格)。
import easygui as g

msg = "请填写以下联系方式"
title = "账号中心"
fieldNames = [" *用户名", " *真实姓名", "固定电话", " *手机号码", "QQ", " *E-mail"]
fieldValues = []
fieldValues = g.multenterbox(msg, title, fieldNames)

while 1:
if fieldValues == None:
      break                                                                            #不太理解标红代码的作用是什么,求解
    errmsg = ""
    for i in range(len(fieldNames)):
      option = fieldNames.strip()
      if fieldValues.strip() == "" and option == "*":
          errmsg += ('【%s】为必填项。\n\n' % fieldNames)
    if errmsg == "":
      break
    fieldValues = g.multenterbox(errmsg, title, fieldNames, fieldValues)

print("用户资料如下:%s" % str(fieldValues))


2.

[*]if choice == "另存为...":
[*]      another_path = g.filesavebox(default=".txt")
[*]      if os.path.splitext(another_path) != '.txt':
[*]            another_path += '.txt'                                        #我的理解是:如果default已经对文件类型进行txt的限制,那么后面两行代码的意义是什么,请鱼友                                                                           帮忙解释一下这三行代码的作用是什么?
[*]      with open(another_path, "w") as new_file:
[*]            new_file.write(text_after[:-1])




3.
写一个程序统计你当前代码量的总和,并显示离十万行代码量还有多远??-D+=mG
RN'o@:cu(O5=&?{t2q>!SK[|<;QPV

[*]要求一:递归搜索各个文件夹
[*]要求二:显示各个类型的源文件和源代码数量
[*]要求三:显示总行数与百分比
https://xxx.ilovefishc.com/forum/201406/12/231223bhahyimbk184z8wh.png


小甲鱼给的答案:
import easygui as g
import os
def show_result(start_dir):
    lines = 0
    total = 0
    text = ""
    for i in source_list:
      lines = source_list
      total += lines
      text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list, lines)
    title = '统计结果'
    msg = '您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力!' % (total, total/1000, 100000-total)
    g.textbox(msg, title, text)
def calc_code(file_name):
    lines = 0
    with open(file_name) as f:
      print('正在分析文件:%s ...' % file_name)
      try:
            for each_line in f:
                lines += 1
      except UnicodeDecodeError:
            pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
    return lines
def search_file(start_dir) :
    os.chdir(start_dir)
   
    for each_file in os.listdir(os.curdir) :
      ext = os.path.splitext(each_file)
      if ext in target :
            lines = calc_code(each_file) # 统计行数
            # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
            # 统计文件数
            try:
                file_list += 1
            except KeyError:                                          ##小甲鱼的解释看不懂,尤其是放大的红字,反正我看不懂……
                                                                               ##想请问一下file_list = 1和source_list=lines是什么意思,在具体的程序运行中起到什么作用咧
                file_list = 1                                        ##还想请问一下source_list = source_list + lines 在实际中怎么解释啊
            # 统计源代码行数
            try:
                source_list += lines
            except KeyError:
                source_list = lines
            
      if os.path.isdir(each_file) :
            search_file(each_file) # 递归调用
            os.chdir(os.pardir) # 递归调用后切记返回上一层目录
            
target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
file_list = {}
source_list = {}
g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
path = g.diropenbox("请选择您的代码库:")
search_file(path)
show_result(path)




一共三个问题,非常感谢帮我解答的大哥!!!!!!!!!!!!!!!
!!!!!究极感谢!!!!!!!!!爱死了!!!!!!!!!!!



wangka 发表于 2020-3-14 07:38:51

红色是自创代码

wangka 发表于 2020-3-14 07:39:43

还有几个是跟进进度条

wangka 发表于 2020-3-14 07:40:27

如果觉得好请最佳
页: [1]
查看完整版本: 035 课后习题关于eaygui几题中的部分代码不太理解