鱼C论坛

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

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

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

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

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

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

错误代码:
judge('file1','file2')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:/Users/86151/Desktop/help.py", line 6, in judge
    judge_file_different()
  File "C:/Users/86151/Desktop/help.py", line 8, in judge_file_different
    for each_line_file1 in file1:
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被屏蔽了  所以报错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-30 23:17:12 | 显示全部楼层
'file1','file2' 不应该是文件路径吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

是  我file1.txt  file2.txt 就是文件名
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

judge('file1','file2')
难道是我眼睛有问题了???怎么没看见.txt
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

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

emmm   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

不想缩进

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


def judge(file1_,file2_):           
    file1 = open(file1_+'.txt',encoding = 'utf-8')
    file2 = open(file2_+'.txt',encoding = 'utf-8')
    count = 0 ; times = 0 ; false = []      #count 记录错误行数 、times 记录错误次数 
    judge_file_different()
    
    def judge_file_different():         # 判断文件不同处
        for each_line_file1 in file1:
            count +=1
            each_line_file2 = file2.readline()
            if each_line_file1 != each_line_file2 :
                false.append(count)         # 将错误的行数整理成列表
                times += 1
        file1.close()
        file2.close()
        if times != 0:
            print('两个文本文件有【%d】处不同:'%times)
            for i in false:
                print('第 %d 行不同'%int(i))
        else:
            print('两文本完全相同')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

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

不想缩进

不行的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

不想缩进

我自己解决了,不用第二个函数删了就OK了
for each_line_file1 in file1 的file1被屏蔽了  所以报错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 02:00:29 From FishC Mobile | 显示全部楼层
啾啾
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-24 19:11:53 | 显示全部楼层
给自己最佳??!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 17:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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