如何在不运行变量的情况下导入等于输入的变量?

时间:2017-04-09 15:09:09

标签: python

我正在为学校做一个项目,我必须创建一个系统来识别笔记本电脑等设备的故障。

最后,我尝试将用户的电子邮件和名称打印到.txt文件中,但是存储这些文件的变量位于另一个python文件中。当我使用 从_____导入_____ 导入变量时,变量运行,因此显示我使用输入函数实现的问题。

请帮助!!

这是一些测试代码

test1:

userName = input ("What is your name ? ")

userEmail = input ("What is your email address? ") 

import test2

test2:

from test1 import userName 
from test1 import userEmail

caseNumber = (random.randint(0,999999))

file = open ("CaseInfo.txt","w")
file.write(userName)
file.write(userEmail)
file.write(caseNumber)

当我运行此而不是正在打印的变量的值时,重新执行该变量,因此再次询问该问题。

1 个答案:

答案 0 :(得分:1)

您的问题可以通过从test1中删除import语句并始终运行 test2来解决

test1:

var arr = ["g", "z", "z", "z" ]; // Must be true

var arr = ["g", "z", "z", "v" ,"b", "b", "b"]; // Must be true

var arr = ["z", "g", "z", "z"]; // Must be false

test2:

userName = input ("What is your name ? ")
userEmail = input ("What is your email address? ") 

#import test2

或test3:

from test1 import userName 
from test1 import userEmail
caseNumber = (random.randint(0,999999))

file = open ("CaseInfo.txt","w")
file.write(userName)
file.write(userEmail)
file.write(caseNumber)