2013-07-07 48 views
0

我在linux機器上使用python 2.7,我確信這是一個明顯的問題,但 我真的需要這個答案。全局名稱'數'沒有在模塊導入時定義

模塊在一個文件名爲一個測試

def one(): 
    number=1 

模塊2在文件兩個名爲考試

def two(): 
    if number == 1: 
     print "apples and oranges" 

和兩者導入到名爲modi.py容器像這樣

import test, exam 

test.one() 
exam.two() 

我希望在一個模塊設置變量「數」,並在模塊兩個基準,但我不斷收到名稱錯誤「全局名稱‘數字’沒有定義」

我做不到看到問題

回答

0

這是因爲number是在test.py中。在exam.py中test.py和import test的第一次使用之前嘗試添加global number。然後使用if test.number == 1:

+0

感謝您使用此工具http://docs.python.org/2/faq/programming.html#how-do-i-share-global-variables-across-modules – user2558735

相關問題