如何导入 Python 文件并稍后使用用户输入?
例如:
#mainprogram
from folder import startup
name
#startup
name = input('Choose your name')
我想要的是使用启动程序输入名称,然后可以在主程序中使用该名称。
如何导入 Python 文件并稍后使用用户输入?
例如:
#mainprogram
from folder import startup
name
#startup
name = input('Choose your name')
我想要的是使用启动程序输入名称,然后可以在主程序中使用该名称。
startup.name
您可以稍后在代码中访问该变量。
name
will be in startup.name
. You can use dir(startup)
to see it.
Or, as an alternate solution:
# Assuming from the names that 'folder' is a folder and 'startup' is a Python script
from folder.startup import *
now you can just use name
without the startup.
in front of it.
我认为最好在类和函数中编写代码。我建议你这样做:
class Startup(object):
@staticmethod
def getName():
name = ""
try:
name = input("Put your name: ")
print('Name took.')
return True
except:
"Can't get name."
return False
>> import startup
>> Startup.getName()