我对编码很新,目前正在学习我的第一语言(python),使用“学习Python艰难之路”一书,但这不是本书中的具体练习,我只是在阅读代码时练习对于练习23而且我现在只想弄清楚这是否可能......
我的第一个文件是pr1.py:
def func():
a = float(raw_input("Enter Your Age:"))
b = float(raw_input("Enter Your Weight:"))
c = float(raw_input("Enter Your Height:"))
age = "a"
weight = "b"
height = "c"
if __name__ == "__main__":
print("This is pr1.py")
else:
print("%s is being imported to another file") % __name__
我的第二个文件是pr2.py
import pr1
def func():
x = raw_input("Enter Your Race:")
y = raw_input("Enter Your Gender:")
z = raw_input("Enter Your Language:")
print "Lets find your numbers"
pr1.func()
print "Lets find your identity"
func()
race = "x"
gender = "y"
language = "z"
if __name__ == "__main__":
print("This is pr2.py")
else:
print("%s is being imported into another file") % __name__
这是我的第三个文件pr3.py
import pr1
print "%s = a %s = b %s = c" % (age, weight, height)
import pr2
print "%s = x, %s = y, %s = z" % (race, gender, language)
当我运行pr3.py并注释掉脚本以“打印”第3行和第7行时,这就是我得到的:
python pr3.py
pr1 is being imported to another file
Lets find your numbers
Enter Your Age:25
Enter Your Weight:224
Enter Your Height:76
Lets find your identity
Enter Your Race:white
Enter Your Gender:male
Enter Your Language:english
pr2 is being imported into another file
我期望pr3.py文件用先前定义的变量打印那些语句。
但它出现了这个错误:
pr1 is being imported to another file
Traceback (most recent call last):
File "pr3.py", line 3, in <module>
print "%s = a %s = b %s = c" % (age, weight, height)
NameError: name 'age' is not defined
现在当我在命令中运行我的最后一个文件时,我希望它能导入前两个文件,所以我可以输入我放入raw_input的数据,然后在其他文件中使用它......但似乎比如一旦两个文件都被导入并且我将数据输入到它们各自的raw_input中,似乎pr3.py忘记了原始输入及其相应的变量。
请原谅我,如果我完全缺乏可以解决所有问题的明显知识,但我对编码很新,甚至一个月前,我甚至不知道你可以在终端创建一个目录。 / p>
感谢您的阅读,我将不胜感激。
答案 0 :(得分:0)
所以,首先:
age, weight, height
中都定义了pr1.py
,因此您需要将其称为pr1.age, pr1.weight, pr1.height
,或from pr1 import age, weight, height
。同样适用于race, gender, language
中的pr2.py
也就是说,您可能希望得到您输入的内容。但您只为这些变量分配了字符'a'
,'b'
,'c'
,'x'
,'y'
和'z'
。
您也可以在age, weight and height
func()
之前打印pr2.py
所以第二:我们如何解决这个问题。首先,当你进行进口时,你需要考虑事情发生的顺序。如果您引用的变量确实在任何地方设置,您还需要考虑
在这种情况下,您将raw_input
设置为函数中的变量,并且只能在该函数内访问它们。
这可以通过使用全局变量或通过制作对象来解决。我将建议使用对象(类)
既然你说你对编程很陌生,那么现在看来你的课程似乎已经过时了,但是当你得到它时,它解决了很多这些问题。
你很快就会在Learn Python The Hard Way很快上课。
以您的代码为例:
pr1.py:
class Numbers:
def func(self):
self.age = float(raw_input("Enter Your Age:"))
self.weight = float(raw_input("Enter Your Weight:"))
self.height = float(raw_input("Enter Your Height:"))
if __name__ == "__main__":
print("This is pr1.py")
else:
print("%s is being imported to another file") % __name__
pr2.py:
from pr1 import Numbers
class Identity:
def func(self):
self.race = raw_input("Enter Your Race:")
self.gender = raw_input("Enter Your Gender:")
self.language = raw_input("Enter Your Language:")
print "Lets find your numbers"
nrs = Numbers()
nrs.func()
print "Lets find your identity"
ids = Identity()
ids.func()
if __name__ == "__main__":
print("This is pr2.py")
else:
print("%s is being imported into another file") % __name__
pr3.py:
from pr2 import *
print "%s = a %s = b %s = c" % (nrs.age, nrs.weight, nrs.height)
print "%s = x, %s = y, %s = z" % (ids.race, ids.gender, ids.language)