如何定义存储在函数内部的字典?

时间:2019-04-29 00:34:40

标签: python-2.7

这是错误

回溯(最近通话最近):   在第19行的文件“ important_page_numbers2.py”中     打印(字典['page_number0']) NameError:名称“字典”未定义


(程序退出,代码为1)

按任意键继续。 。

我正在尝试从另一个程序中发布的代码中导入特定功能。我不希望程序完全执行,因为它还会打印。

为了解决这个问题,我将不同的动作分为函数bc,我知道在2.7.15中,需要在函数外部键入函数名称才能使用。

基本上,当我使第一个字典成为函数时,它不起作用

list1=["pg38", "page62", "83", "86", "96", "126", "page210", "236", 
"page155", "page 324"]#Hey stack overflow,
# this part doesn't matter I just never removed it bc i might use it 
#later
def dicts():
dicctionary={"page_number0":"pg38","description0":"Working with dictionaries",
 "page_number1":"page62", "description1":"Working with ranges",
  "page_number2":"83","description2":"If and else sattements",
   "page_number3":"86", "description3":"omitting the else block",
    "page_number4":"96", "description4":"working with dictionaries",
     "page_number5":"126","description5":"Using continue in a loop",
     "page_number6":"page155","description6":"Importing an entire module",
     "page_number6.1":"page189","description6.1":"Chapter 10, files and exceptions",
      "page_number7":"page210","description7":"Saving user generated data",
      "page_number8":"236","description8":"Installing pygame",
       "page_number9":"page 324","description9":"Matplotlib"}

dicts()
def print1():
print(dicctionary['page_number0'])
print(dicctionary['description0'])
print("\n")
print(dicctionary['page_number1'])
print(dicctionary['description1'])
print("\n")
print(dicctionary['page_number2'])
print(dicctionary['description2'])
print("\n")
print(dicctionary['page_number3'])
print(dicctionary['description3'])
print("\n") 
print(dicctionary['page_number4'])
print(dicctionary['description4'])
print("\n")
print(dicctionary['page_number5'])
print(dicctionary['description5'])
print("\n")
print(dicctionary['page_number6'])
print(dicctionary['description6'])
print("\n")
print(dicctionary['page_number6.1'])
print(dicctionary['description6.1'])
print("\n")
print(dicctionary['page_number7'])
print(dicctionary['description7'])
print("\n")     
print(dicctionary['page_number8'])
print(dicctionary['description8'])
print("\n")
print(dicctionary['page_number9'])
print(dicctionary['description9'])                          



print1()

我希望能够使用import来调用特定功能,而不会在我自行编译原始文件时停止打印原始文件。

当我将字典设为函数时,python在尝试打印时无法识别它。

2 个答案:

答案 0 :(得分:1)

您不能从外部代码访问仅在函数中定义为局部变量的变量。这是功能的要点之一,它们具有自己的名称空间。有几种方法可以解决此问题。

一个函数可以修改其定义的模块的全局名称空间。要在全局Namsapce中分配新变量,您需要在函数内部放置global语句。例如,您可以将global dictionary放在大字典定义的上方,这样就可以工作(在调用函数时定义字典)。

但是使用全局变量有一个缺点,那就是您必须将要使用的变量名称直接编程到函数中。如果您希望能够两次调用该函数,并获得两个单独的字典,那么您将很难过,因为第二个将覆盖第一个。

因此,从函数中获取数据的更好方法是return。您在函数中的return之后命名的值将用作函数调用的值。因此,调用者可以对所需的任何变量进行自己的分配(甚至可以在单独的模块中进行分配)。因此,请考虑将return dictionary添加到函数中,然后通过将调用放入赋值语句中来使用它:foo = dicts()

答案 1 :(得分:0)

您缺少dicts()函数中的return语句和赋值语句。那是一个未定义的错误。

执行以下操作:

def dicts():
    return ({"page_number0":"pg38","description0":"Working with dictionaries",
     "page_number1":"page62", "description1":"Working with ranges",
      "page_number2":"83","description2":"If and else sattements",
       "page_number3":"86", "description3":"omitting the else block",
        "page_number4":"96", "description4":"working with dictionaries",
         "page_number5":"126","description5":"Using continue in a loop",
         "page_number6":"page155","description6":"Importing an entire module",
         "page_number6.1":"page189","description6.1":"Chapter 10, files and exceptions",
          "page_number7":"page210","description7":"Saving user generated data",
          "page_number8":"236","description8":"Installing pygame",
           "page_number9":"page 324","description9":"Matplotlib"})

dicctionary = dicts()