【发布时间】:2017-07-07 11:45:40
【问题描述】:
我对编码还很陌生,目前正在使用“Learn Python the Hard Way”一书学习我的第一语言(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 似乎忘记了原始输入及其对应的变量。
如果我完全缺乏一些可以解决所有这些问题的明显知识,请原谅我,但我对编码非常陌生,甚至不到一个月前,我什至不知道你可以在终端中创建一个目录。
感谢您的阅读,我将不胜感激。
【问题讨论】:
标签: macos python-2.7 atom-editor