2017-04-12 205 views
0

我有我正在使用的兩個python文件。Python模塊/導入數據?

在cryp.py我:

def test(): 
    test = raw_input("> ") 

在cryptek.py我:

import cryp 
What is your name? 
cryp.test 
print "To be sure your name is: " + test 

它errrs出爲 「沒有定義的測試」 我如何使它獲得測試變量來自cryp.py?

+0

您是否嘗試過'進口cryp'? – lit

+0

是的,我已經在頂端導入cryp我將在 – TheCryptek

+0

編輯它'test'是Python中的保留關鍵字嗎? – lit

回答

0

(我有Python 3.6.1)所以我解決了它,但不完全按照你的要求。但願這給你一個開始:

cryptek:

from cryp import * 
print("To be sure your name is: " + test) 

cryp:

name = input ("> ") 
def test(): 
    pass 
test() 

運行完美。問題在於定義的來源。

您是否需要此操作符合定義?

+1

我已經試過gmons,它仍然給我「測試沒有定義」我使用導入cryp已經 – TheCryptek

+0

這些帖子有幫助嗎? http://stackoverflow.com/questions/14573021/python-using-variables-from-another-file http://stackoverflow.com/questions/2349991/python-how-to-import-other-python - 文件 http://stackoverflow.com/questions/17255737/importing-variables-from-another-file – gmonz

+1

不,它實際上沒有。在發佈這個問題之前,我審視了這個問題,因爲從技術上講,這是一個規則。 – TheCryptek

0

這裏有很多問題。您添加了import聲明。 test()函數需要返回一些東西。如果不是,它將返回None。另外,cryp.test是函數對象,而cryp.test()是對該函數的調用。

不在問題中的錯誤消息說明了這一點。

Traceback (most recent call last): 
    File "cryptek.py", line 4, in <module> 
    print "To be sure your name is: " + cryp.test() 
TypeError: cannot concatenate 'str' and 'NoneType' objects 

cryp.py

def test(): 
    test = raw_input("> ") 
    return test 

cryptek.py

import cryp 
print "What is your name?" 
cryp.test 
print "To be sure your name is: " + cryp.test() 

C:>python cryptek.py 
What is your name? 
> asdf 
To be sure your name is: asdf 
+0

添加完成後,它現在會以test = raw_input(「>」)兩次提示我,這意味着我必須在繼續輸入名稱兩次之前輸入一個名稱。 – TheCryptek

+0

那不是我的錯誤......這是我的錯誤 ' 回溯(最近通話最後一個): 文件「cryptek.py」,4號線,在 打印「\ n要肯定,你的名字是: 「+ name NameError:name'name'is not defined ' – TheCryptek

+0

@TheCryptek - 對不起,是的,你有錯誤信息。 'can not concatenate'消息是你下一次得到的消息,直到'()'被添加到'test'以使其成爲一個函數調用。 – lit