鱼C论坛

 找回密码
 立即注册
查看: 2491|回复: 5

[已解决]python 30讲(老版) 课后作业4

[复制链接]
发表于 2021-2-8 14:48:52 | 显示全部楼层 |阅读模式
5鱼币
各位大佬,请问为什么会出现这样的情况?

23、24行报错:invalid syntax

30动动手4.png

代码如下:

  1. def key_find(each_line,key,count):
  2.     if count>0:
  3.         t = each_line.index(key)
  4.         position.append(t)
  5.         key_search(each_line[t+len(key):],key,count-1)
  6.         
  7. def result_find(path,each_file,key):
  8.     f = open(path+'\\'+each_file)
  9.     content = []
  10.     for each_line in f:
  11.         content.append(each_line)
  12.     for each_line in content:
  13.         if key in each_line:
  14.             print('在文件【%s】中找到关键字【%s】' %(path+'\\'+each_file,key))
  15.             count = each_line.count(key)
  16.             if count == 1:
  17.                 position = each_line.index(key)
  18.                 print('关键字出现在第 %d 行,第 %d 个位置' %(content.index(each_line)+1,position))
  19.             else:
  20.                 position = []
  21.                 key_search(each_line,key,count)
  22.                 print('关键字出现在第 %d 行,第 %s 个位置' %(content.index(each_line)+1,str(position))

  23. def goalfile_find(path,key):
  24.     all_file = os.listdir(path)
  25.     print('='*10)
  26.     for each_file in all_file:
  27.         (name,ext)  =  os.path.splitext(path+'\\'+each_file)
  28.         if ext == extension:
  29.             result_find(path,each_file,key)
  30.         if os.path.isdir(path+'\\'+each_file):
  31.             goalfile_find(path+'\\'+each_file,key)

  32. import os
  33. import os.path

  34. extension = '.txt'

  35. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  36. choose = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)

  37. if choose in ['YES','Yes','yes']:
  38.     path = os.getcwd()
  39.                               
  40. goalfile_find(path,key)               
复制代码


新手求解答和解决办法,谢谢
最佳答案
2021-2-8 14:48:53
  1. def key_find(each_line,key,count):
  2.     if count>0:
  3.         t = each_line.index(key)
  4.         position.append(t)
  5.         key_search(each_line[t+len(key):],key,count-1)
  6.         
  7. def result_find(path,each_file,key):
  8.     f = open(path+'\\'+each_file)
  9.     content = []
  10.     for each_line in f:
  11.         content.append(each_line)
  12.     for each_line in content:
  13.         if key in each_line:
  14.             print('在文件【%s】中找到关键字【%s】' %(path+'\\'+each_file,key))
  15.             count = each_line.count(key)
  16.             if count == 1:
  17.                 position = each_line.index(key)
  18.                 print('关键字出现在第 %d 行,第 %d 个位置' %(content.index(each_line)+1,position))
  19.             else:
  20.                 position = []
  21.                 key_search(each_line,key,count)
  22.                 print('关键字出现在第 %d 行,第 %s 个位置' %(content.index(each_line)+1,str(position)))

  23. def goalfile_find(path,key):
  24.     all_file = os.listdir(path)
  25.     print('='*10)
  26.     for each_file in all_file:
  27.         (name,ext)  =  os.path.splitext(path+'\\'+each_file)
  28.         if ext == extension:
  29.             result_find(path,each_file,key)
  30.         if os.path.isdir(path+'\\'+each_file):
  31.             goalfile_find(path+'\\'+each_file,key)

  32. import os
  33. import os.path

  34. extension = '.txt'

  35. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  36. choose = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)

  37. if choose in ['YES','Yes','yes']:
  38.     path = os.getcwd()
  39.                               
  40. goalfile_find(path,key)               
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-8 14:48:53 | 显示全部楼层    本楼为最佳答案   
  1. def key_find(each_line,key,count):
  2.     if count>0:
  3.         t = each_line.index(key)
  4.         position.append(t)
  5.         key_search(each_line[t+len(key):],key,count-1)
  6.         
  7. def result_find(path,each_file,key):
  8.     f = open(path+'\\'+each_file)
  9.     content = []
  10.     for each_line in f:
  11.         content.append(each_line)
  12.     for each_line in content:
  13.         if key in each_line:
  14.             print('在文件【%s】中找到关键字【%s】' %(path+'\\'+each_file,key))
  15.             count = each_line.count(key)
  16.             if count == 1:
  17.                 position = each_line.index(key)
  18.                 print('关键字出现在第 %d 行,第 %d 个位置' %(content.index(each_line)+1,position))
  19.             else:
  20.                 position = []
  21.                 key_search(each_line,key,count)
  22.                 print('关键字出现在第 %d 行,第 %s 个位置' %(content.index(each_line)+1,str(position)))

  23. def goalfile_find(path,key):
  24.     all_file = os.listdir(path)
  25.     print('='*10)
  26.     for each_file in all_file:
  27.         (name,ext)  =  os.path.splitext(path+'\\'+each_file)
  28.         if ext == extension:
  29.             result_find(path,each_file,key)
  30.         if os.path.isdir(path+'\\'+each_file):
  31.             goalfile_find(path+'\\'+each_file,key)

  32. import os
  33. import os.path

  34. extension = '.txt'

  35. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  36. choose = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)

  37. if choose in ['YES','Yes','yes']:
  38.     path = os.getcwd()
  39.                               
  40. goalfile_find(path,key)               
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-8 14:51:53 | 显示全部楼层
少打个括号

  1. def key_find(each_line,key,count):
  2.     if count>0:
  3.         t = each_line.index(key)
  4.         position.append(t)
  5.         key_search(each_line[t+len(key):],key,count-1)
  6.         
  7. def result_find(path,each_file,key):
  8.     f = open(path+'\\'+each_file)
  9.     content = []
  10.     for each_line in f:
  11.         content.append(each_line)
  12.     for each_line in content:
  13.         if key in each_line:
  14.             print('在文件【%s】中找到关键字【%s】' %(path+'\\'+each_file,key))
  15.             count = each_line.count(key)
  16.             if count == 1:
  17.                 position = each_line.index(key)
  18.                 print('关键字出现在第 %d 行,第 %d 个位置' %(content.index(each_line)+1,position))
  19.             else:
  20.                 position = []
  21.                 key_search(each_line,key,count)
  22.                 print('关键字出现在第 %d 行,第 %s 个位置' %(content.index(each_line)+1,str(position)))
  23. def goalfile_find(path,key):
  24.     all_file = os.listdir(path)
  25.     print('='*10)
  26.     for each_file in all_file:
  27.         (name,ext)  =  os.path.splitext(path+'\\'+each_file)
  28.         if ext == extension:
  29.             result_find(path,each_file,key)
  30.         if os.path.isdir(path+'\\'+each_file):
  31.             goalfile_find(path+'\\'+each_file,key)

  32. import os
  33. import os.path

  34. extension = '.txt'

  35. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  36. choose = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)

  37. if choose in ['YES','Yes','yes']:
  38.     path = os.getcwd()
  39.                               
  40. goalfile_find(path,key)               
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-8 14:55:31 | 显示全部楼层
楼主粗心了~函数上面那一句打印少了个括号
楼上正确代码
望采纳
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-2-8 20:10:37 | 显示全部楼层

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-2-8 20:11:40 | 显示全部楼层
青出于蓝 发表于 2021-2-8 14:55
楼主粗心了~函数上面那一句打印少了个括号
楼上正确代码
望采纳

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-2 22:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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