鱼C论坛

 找回密码
 立即注册
查看: 1455|回复: 3

[已解决]零基础入门学python035讲动动手第4题

[复制链接]
发表于 2018-4-6 20:22:15 | 显示全部楼层 |阅读模式

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

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

x
  1. #/usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. import easygui as g
  4. import os

  5. def calculation(file1):
  6.         file_lines=0
  7.         with open(file1) as f:
  8.                 print '正在分析文件:%s'% file1
  9.                 try:
  10.                         for eachline in f:
  11.                                 file_lines+=1
  12.                 except UnicodeDecodeError:
  13.                         pass
  14.         return file_lines

  15. def search(directory):
  16.         os.chdir(directory)


  17.         for eachfile in os.listdir(os.chdir):
  18.                 ext=os.path.splitext(eachfile)[1]
  19.                 if ext in ['.py']:
  20.                         lines=calculation(eachfile)
  21.                         try:
  22.                                 allfile[ext]+=1
  23.                         except KeyError:
  24.                                 allfile[ext]=1
  25.                         try:
  26.                                 alllines[ext]+=lines
  27.                         except KeyError:
  28.                                 alllines[ext]=lines
  29.                 if os.path.isdir(eachfile):
  30.                         search(eachfile)
  31.                         os.chdir(os.pardir)

  32. def show(directory):
  33.         text=''
  34.         total_codes_lines=0

  35.         for eachkey in alllines:
  36.                 linesssss=alllines[eachkey]
  37.                 text+='【%s】源文件有%d个,代码一共%d行\n'%(eachkey,allfile[eachkey],linesssss)
  38.                 total_codes_lines+=alllines[eachkey]
  39.         msg='您目前所有代码量为%d行,完成进度为%.2f%% \n离10万行代码还差%d行,继续努力呦!' %(total_codes_lines,total_codes_lines/1000,100000-total_codes_lines)
  40.         g.textbox(msg,'统计结果',text)

  41. allfile={}
  42. alllines={}
  43. g.msgbox('请打开您要检查的文件夹','统计代码量')
  44. path=g.diropenbox('请选择一个文件夹!')

  45. search(path)
  46. show(path)
复制代码


请问(1)第44行为什么报错?

    text+='【%s】源文件有%d个,代码一共%d行\n'%(eachkey,allfile[eachkey],linesssss)
                                                            ^
SyntaxError: invalid syntax
[Finished in 0.0s with exit code 1]

(2)关于show函数,感觉函数的定义(第38行)和调用(第55行)时都不需要参数,因为函数中没有用到......所以这两个参数我是不是可以不写?
最佳答案
2018-4-6 21:01:30
第44行的错误是因为你的括号前半个不是英文输入的。。。包括46行和51行都有这个问题。。。
至于你说的那两个参数我感觉确实可以不要,不过你的alllines和allfile这两个参数要传入函数啊,不然函数找不到那两个字典还是报错的。。。
我把你的代码这样改了之后好像还是有点问题,你自己再看看吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-6 20:23:02 | 显示全部楼层
原题如下:
动动手 4.写一个程序统计你当前的代码量总和,并显示离十万行代码量还差多远?
要求1: 递归搜索各个文件夹
要求2:显示各个类型的源文件和源代码数量
要求3:显示总行数与百分比
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-6 21:01:30 | 显示全部楼层    本楼为最佳答案   
第44行的错误是因为你的括号前半个不是英文输入的。。。包括46行和51行都有这个问题。。。
至于你说的那两个参数我感觉确实可以不要,不过你的alllines和allfile这两个参数要传入函数啊,不然函数找不到那两个字典还是报错的。。。
我把你的代码这样改了之后好像还是有点问题,你自己再看看吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-6 22:39:25 | 显示全部楼层
Luke李 发表于 2018-4-6 21:01
第44行的错误是因为你的括号前半个不是英文输入的。。。包括46行和51行都有这个问题。。。
至于你说的那两 ...
  1. #/usr/bin/env python
  2. # -*- coding:utf-8 -*-

  3. import easygui as g
  4. import os

  5. def calculation(file1):
  6.         file_lines=0
  7.         with open(file1) as f:
  8.                 print '正在分析文件:%s'% file1
  9.                 try:
  10.                         for eachline in f:
  11.                                 file_lines+=1
  12.                 except UnicodeDecodeError:
  13.                         pass
  14.         return file_lines

  15. def search(directory):
  16.         os.chdir(directory)
  17.         for eachfile in os.listdir(os.curdir):
  18.                 ext=os.path.splitext(eachfile)[1]
  19.                 if ext in ['.py']:
  20.                         lines=calculation(eachfile)
  21.                         try:
  22.                                 allfile[ext]+=1
  23.                         except KeyError:
  24.                                 allfile[ext]=1
  25.                         try:
  26.                                 alllines[ext]+=lines
  27.                         except KeyError:
  28.                                 alllines[ext]=lines
  29.                 if os.path.isdir(eachfile):
  30.                         search(eachfile)
  31.                         os.chdir(os.pardir)

  32. def show():
  33.         text=''
  34.         total_codes_lines=0

  35.         for eachkey in alllines:
  36.                 linesssss=alllines[eachkey]
  37.                 text+='【%s】源文件有%d个,代码一共%d行\n'%(eachkey,allfile[eachkey],linesssss)
  38.                 total_codes_lines+=alllines[eachkey]
  39.         msg='您目前所有代码量为%d行,完成进度为%.2f%% \n离10万行代码还差%d行,继续努力呦!' %(total_codes_lines,total_codes_lines/1000,100000-total_codes_lines)
  40.         g.textbox(msg,'统计结果',text)

  41. allfile={}
  42. alllines={}
  43. g.msgbox('请打开您要检查的文件夹','统计代码量')
  44. path=g.diropenbox('请选择一个文件夹!')

  45. search(path)
  46. show()
复制代码


谢谢!我把几个你指出的语法错误改了,然后allfile和alllines还是放在函数外,show不带参数,运行时,报了coercing to Unicode错误,发现是把for eachfile in os.listdir(os.curdir):误写成了for eachfile in os.listdir(os.chdir):

修正后就没有问题了~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-10 07:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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