鱼C论坛

 找回密码
 立即注册
查看: 1041|回复: 1

[已解决]easygui课后题的最后一道题,我用的笨办法结果全是0,可以帮我指出问题在哪里吗

[复制链接]
发表于 2022-3-21 16:03:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import easygui as g
import os
code_type=['.py','.c','.cpp','.pas','.asm']
countc=countcpp=countpas=countasm=countpy=0
numpy=numc=numcpp=numasm=numpas=0
num_file=[numpy,numc,numcpp,numpas,numasm]
num_line=[countc,countcpp,countpas,countasm,countpy]


def find_code():
   
    all_file=os.listdir('.')
    for each_file in all_file:
        if os.path.isfile(each_file):
            if os.path.splitext(each_file)[1]==code_type[0]:
                global numpy
                numpy+=1
                with open(each_file,encoding='utf-8') as f:
                    for each_line in f:
                        global countpy
                       
                        countpy+=1
            if os.path.splitext(each_file)[1]==code_type[1]:
                global numc
                numc+=1
                with open(each_file,encoding='utf-8') as f:
                    for each_line in f:
                        global countc
                       
                        countc+=1
            if os.path.splitext(each_file)[1]==code_type[2]:
                global numcpp
                numcpp+=1
                with open(each_file,encoding='utf-8') as f:
                    for each_line in f:
                        global countcpp
                       
                        countcpp+=1
            if os.path.splitext(each_file)[1]==code_type[3]:
                global numpas
                numpas+=1
                with open(each_file,encoding='utf-8') as f:
                    for each_line in f:
                        global countpas
                       
                        countpas+=1
            if os.path.splitext(each_file)[1]==code_type[4]:
                global numasm
                numasm+=1
                with open(each_file,encoding='utf-8') as f:
                    for each_line in f:
                        global countasm
                       
                        countasm+=1

        if os.path.isdir(each_file):
            os.chdir(each_file)
            find_code()
            os.chdir(os.pardir)

g.msgbox('请打开你存放代码的文件夹')
path=g.diropenbox('请选择你存放代码的文件夹')
os.chdir(path)
find_code()
for each in num_line:
    sum=0+each
spare=100000-sum
proportion=sum/100000
msg='您目前共累计编写了%d条代码,完成进度:%.2f\n离十万行代码还差%d行,请继续努力'%(sum,proportion,spare)
title='统计结果'
text=''



for i in range(5):
    text+='%s源文件%d个,原代码%d行/n'%(code_type[i],num_file[i],num_line[i])
g.textbox(msg,title,text)



最佳答案
2022-3-21 19:13:00
问题出在这:
  1. num_file=[numpy,numc,numcpp,numpas,numasm]
  2. num_line=[countc,countcpp,countpas,countasm,countpy]
复制代码

你把变量放进去,传递的只是值
也就是说,你对那些变量进行操作是不会改变列表里的值的。
你可以直接改变列表的值,不用变量。
QQ图片20220321160145.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 19:13:00 | 显示全部楼层    本楼为最佳答案   
问题出在这:
  1. num_file=[numpy,numc,numcpp,numpas,numasm]
  2. num_line=[countc,countcpp,countpas,countasm,countpy]
复制代码

你把变量放进去,传递的只是值
也就是说,你对那些变量进行操作是不会改变列表里的值的。
你可以直接改变列表的值,不用变量。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-6 02:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表