我有我正在使用的两个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?
我有我正在使用的两个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?
(我有Python 3.6.1)所以我解决了它,但不完全按照你的要求。但愿这给你一个开始:
cryptek:
from cryp import *
print("To be sure your name is: " + test)
cryp:
name = input ("> ")
def test():
pass
test()
运行完美。问题在于定义的来源。
您是否需要此操作符合定义?
我已经试过gmons,它仍然给我“测试没有定义”我使用导入cryp已经 – TheCryptek
这些帖子有帮助吗? 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
不,它实际上没有。在发布这个问题之前,我审视了这个问题,因为从技术上讲,这是一个规则。 – TheCryptek
这里有很多问题。您添加了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
添加完成后,它现在会以test = raw_input(“>”)两次提示我,这意味着我必须在继续输入名称两次之前输入一个名称。 – TheCryptek
那不是我的错误......这是我的错误 ' 回溯(最近通话最后一个): 文件“cryptek.py”,4号线,在
@TheCryptek - 对不起,是的,你有错误信息。 'can not concatenate'消息是你下一次得到的消息,直到'()'被添加到'test'以使其成为一个函数调用。 – lit
您是否尝试过'进口cryp'? – lit
是的,我已经在顶端导入cryp我将在 – TheCryptek
编辑它'test'是Python中的保留关键字吗? – lit