草庐IT

internet-explorer-global-variable

全部标签

python - 名称错误 : global name 'reduce' is not defined

我是Python新手。你能告诉我下面的代码有什么问题吗?当我运行它时,我收到一条错误消息“NameError:未定义全局名称'reduce'”。我问过古尔格,但没用。:(defmain():defadd(x,y):returnx+yreduce(add,range(1,11))if__name__=='__main__':main() 最佳答案 我猜:您使用的是Python3,并且您正在学习专为Python2设计的教程。reduce函数由于不常用,在Python3的内置函数中被移除。它仍然在functools模块中可用,所以你可以这

Python 尝试/除 : Showing the cause of the error after displaying my variables

我什至不确定要搜索什么合适的词。我想在exceptblock中显示部分错误对象(类似于VBScript中的err对象,它具有Err.Number和Err.Description)。例如,我想显示我的变量的值,然后显示确切的错误。显然,我在下面导致了一个被零除的错误,但是我怎样才能打印出这个事实呢?try:x=0y=1z=y/xz=z+1print"z=%d"%(z)except:print"ValuesatException:x=%dy=%d"%(x,y)print"Theerrorwasonline..."print"Thereasonfortheerrorwas..."

Python 尝试/除 : Showing the cause of the error after displaying my variables

我什至不确定要搜索什么合适的词。我想在exceptblock中显示部分错误对象(类似于VBScript中的err对象,它具有Err.Number和Err.Description)。例如,我想显示我的变量的值,然后显示确切的错误。显然,我在下面导致了一个被零除的错误,但是我怎样才能打印出这个事实呢?try:x=0y=1z=y/xz=z+1print"z=%d"%(z)except:print"ValuesatException:x=%dy=%d"%(x,y)print"Theerrorwasonline..."print"Thereasonfortheerrorwas..."

python - 从父函数 : "Local variable referenced before assignment" 分配给变量

这个问题在这里已经有了答案:nonlocalkeywordinPython2.x(10个回答)Isitpossibletomodifyavariableinpythonthatisinanouter(enclosing),butnotglobal,scope?(9个回答)关闭8年前。对于以下Python2.7代码:#!/usr/bin/pythondeffunc_a():print"func_a"c=0deffunc_b():c+=3print"func_b",cdeffunc_c():print"func_c",cprint"c",cfunc_b()c+=2func_c()c+=2f

python - 从父函数 : "Local variable referenced before assignment" 分配给变量

这个问题在这里已经有了答案:nonlocalkeywordinPython2.x(10个回答)Isitpossibletomodifyavariableinpythonthatisinanouter(enclosing),butnotglobal,scope?(9个回答)关闭8年前。对于以下Python2.7代码:#!/usr/bin/pythondeffunc_a():print"func_a"c=0deffunc_b():c+=3print"func_b",cdeffunc_c():print"func_c",cprint"c",cfunc_b()c+=2func_c()c+=2f

python - 访问函数外部的函数变量而不使用 "global"

这个问题在这里已经有了答案:HowdoIgetaresult(output)fromafunction?HowcanIusetheresultlater?(4个回答)关闭25天前。我正在尝试在Python中访问函数外部的局部函数变量。我可以使这样的代码与全局变量一起工作:bye=''defhi():globalbyebye=5sigh=10hi()print(bye)接下来,我尝试了这段代码,希望在不使用globalbye的情况下,在hi()之外访问bye:defhi():bye=5sigh=10returnhi()x=hi()print(x.bye)这给出了AttributeErro

python - 访问函数外部的函数变量而不使用 "global"

这个问题在这里已经有了答案:HowdoIgetaresult(output)fromafunction?HowcanIusetheresultlater?(4个回答)关闭25天前。我正在尝试在Python中访问函数外部的局部函数变量。我可以使这样的代码与全局变量一起工作:bye=''defhi():globalbyebye=5sigh=10hi()print(bye)接下来,我尝试了这段代码,希望在不使用globalbye的情况下,在hi()之外访问bye:defhi():bye=5sigh=10returnhi()x=hi()print(x.bye)这给出了AttributeErro

python - if 语句后的 "UnboundLocalError: local variable referenced before assignment"

我也尝试过寻找答案,但我不明白其他人类似问题的答案...tfile=open("/home/path/to/file",'r')deftemp_sky(lreq,breq):forlineintfile:data=line.split()if(abs(float(data[0])-lreq)我收到以下错误7.37052488Traceback(mostrecentcalllast):File"tsky.py",line25,inprinttemp_sky(10,-10)File"tsky.py",line22,intemp_skyreturnTUnboundLocalError:loc

python - if 语句后的 "UnboundLocalError: local variable referenced before assignment"

我也尝试过寻找答案,但我不明白其他人类似问题的答案...tfile=open("/home/path/to/file",'r')deftemp_sky(lreq,breq):forlineintfile:data=line.split()if(abs(float(data[0])-lreq)我收到以下错误7.37052488Traceback(mostrecentcalllast):File"tsky.py",line25,inprinttemp_sky(10,-10)File"tsky.py",line22,intemp_skyreturnTUnboundLocalError:loc

Python:在 Explorer/Nautilus/Finder 中打开文件夹

我在Python中,我有某个文件夹的路径。我想使用该系统的默认文件夹资源管理器打开它。例如,如果是Windows计算机,我想使用Explorer,如果是Linux,我想使用Nautilus或任何默认设置,如果是Mac,我想使用Finder。我该怎么做? 最佳答案 我很惊讶没有人提到将xdg-open用于*nix,它适用于文件和文件夹:importosimportplatformimportsubprocessdefopen_file(path):ifplatform.system()=="Windows":os.startfile(