鱼C论坛

 找回密码
 立即注册
查看: 2734|回复: 9

[已解决]为什么 函数参数变量没定义

[复制链接]
发表于 2020-3-30 23:03:09 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Twilight6 于 2020-3-31 09:46 编辑
  1. def judge(file1_,file2_):           
  2.     file1 = open(file1_+'.txt',encoding = 'utf-8')
  3.     file2 = open(file2_+'.txt',encoding = 'utf-8')
  4.     count = 0 ; times = 0 ; false = []      #count 记录错误行数 、times 记录错误次数
  5.     judge_file_different()
  6. def judge_file_different():         # 判断文件不同处
  7.     for each_line_file1 in file1:
  8.         count +=1
  9.         each_line_file2 = file2.readline()
  10.         if each_line_file1 != each_line_file2 :
  11.             false.append(count)         # 将错误的行数整理成列表
  12.             times += 1
  13.     file1.close()
  14.     file2.close()
  15.     if times != 0:
  16.         print('两个文本文件有【%d】处不同:'%times)
  17.         for i in false:
  18.             print('第 %d 行不同'%int(i))
  19.     else:
  20.         print('两文本完全相同')
复制代码


错误代码:
  1. judge('file1','file2')
  2. Traceback (most recent call last):
  3.   File "<input>", line 1, in <module>
  4.   File "C:/Users/86151/Desktop/help.py", line 6, in judge
  5.     judge_file_different()
  6.   File "C:/Users/86151/Desktop/help.py", line 8, in judge_file_different
  7.     for each_line_file1 in file1:
  8. NameError: name 'file1' is not defined
复制代码




为什么这样调用会说file1,file2没定义?
是局部变量和全局变量的原因吗?

最佳答案
2020-3-31 09:45:42
ba21 发表于 2020-3-31 00:07
这真是神一样的代码。。

不想缩进

我自己解决了,不用第二个函数删了就OK了
for each_line_file1 in file1 的file1被屏蔽了  所以报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-30 23:17:12 | 显示全部楼层
'file1','file2' 不应该是文件路径吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-30 23:20:40 | 显示全部楼层
ba21 发表于 2020-3-30 23:17
'file1','file2' 不应该是文件路径吗?

是  我file1.txt  file2.txt 就是文件名
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-30 23:44:05 | 显示全部楼层
Twilight6 发表于 2020-3-30 23:20
是  我file1.txt  file2.txt 就是文件名

judge('file1','file2')
难道是我眼睛有问题了???怎么没看见.txt
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2020-3-30 23:57:17 | 显示全部楼层
ba21 发表于 2020-3-30 23:44
judge('file1','file2')
难道是我眼睛有问题了???怎么没看见.txt
  1. def judge(file1_,file2_):           
  2.     file1 = open(file1_+'.txt',encoding = 'utf-8')
  3.     file2 = open(file2_+'.txt',encoding = 'utf-8')
复制代码


emmm   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-31 00:07:07 | 显示全部楼层
这真是神一样的代码。。

不想缩进

就把 judge_file_different(file1, file2) 传参进去



  1. def judge(file1_,file2_):           
  2.     file1 = open(file1_+'.txt',encoding = 'utf-8')
  3.     file2 = open(file2_+'.txt',encoding = 'utf-8')
  4.     count = 0 ; times = 0 ; false = []      #count 记录错误行数 、times 记录错误次数
  5.     judge_file_different()
  6.    
  7.     def judge_file_different():         # 判断文件不同处
  8.         for each_line_file1 in file1:
  9.             count +=1
  10.             each_line_file2 = file2.readline()
  11.             if each_line_file1 != each_line_file2 :
  12.                 false.append(count)         # 将错误的行数整理成列表
  13.                 times += 1
  14.         file1.close()
  15.         file2.close()
  16.         if times != 0:
  17.             print('两个文本文件有【%d】处不同:'%times)
  18.             for i in false:
  19.                 print('第 %d 行不同'%int(i))
  20.         else:
  21.             print('两文本完全相同')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2020-3-31 09:35:58 | 显示全部楼层
ba21 发表于 2020-3-31 00:07
这真是神一样的代码。。

不想缩进

不行的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-31 09:45:42 | 显示全部楼层    本楼为最佳答案   
ba21 发表于 2020-3-31 00:07
这真是神一样的代码。。

不想缩进

我自己解决了,不用第二个函数删了就OK了
for each_line_file1 in file1 的file1被屏蔽了  所以报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 02:00:29 From FishC Mobile | 显示全部楼层
啾啾
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-24 19:11:53 | 显示全部楼层
给自己最佳??!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 20:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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