【问题标题】:How to import defined variable from raw_input in an imported file?如何从导入文件中的 raw_input 导入定义的变量?
【发布时间】: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


    【解决方案1】:

    好的,这里有几件事。
    首先,我将说明您的代码有哪些明显的问题。
    之后,我将向您展示使用类的代码示例。

    所以,首先:
    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' 分配给这些变量。
    你甚至在pr2.py 中运行func() 之前打印age, weight and height

    第二个:我们如何解决这个问题。首先,您需要考虑在进行导入时事情发生的顺序。您还需要考虑,如果您引用的变量真的设置在任何地方。
    在这种情况下,您将 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)
    

    【讨论】:

    • 感谢您花时间进行深入的解释,这真的很有帮助!
    • @Niles 你好。
    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多