Skip to main content
 首页 » 编程设计

python之全局找不到名称代码错误

2025年05月04日9cyq1162

def choose(): 
  file = pickAFile() 
  pic = makePicture(file) 
  return pic 
 
def negative(pic): 
  for p in getPixels(pic): 
    setRed(p, 255 - getRed(p)) 
    setGreen(p, 255 - getGreen(p)) 
    setBlue(p, 255 - getBlue(p)) 
  explore(pic) 
 
def main(): 
  choose() 
  negative(pic) 

我收到这个错误:

The error was:pic Name not found globally. A local or global name could not be found. You need to define the function or variable before you try to use it in any way. Please check line 15 of C:\Users\nh_2109\Desktop\s

求助?:D

请您参考如下方法:

您还没有在 main 方法中定义 pic。

def main(): 
   pic = choose() 
   negative(pic) 

可能是您想要的,因为 choose() 返回一个 Pic 类的实例。