0

如何导入 Python 文件并稍后使用用户输入?

例如:

#mainprogram
from folder import startup
name

#startup
name = input('Choose your name')

我想要的是使用启动程序输入名称,然后可以在主程序中使用该名称。

4

3 回答 3

2

startup.name您可以稍后在代码中访问该变量。

于 2013-08-10T20:46:25.143 回答
0

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.

于 2013-08-10T20:50:35.323 回答
0

我认为最好在类和函数中编写代码。我建议你这样做:

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()
于 2013-08-10T23:27:50.317 回答