Reputation: 293
How do I import a Python file and use the user input later?
For example:
#mainprogram
from folder import startup
name
#startup
name = input('Choose your name')
What I want is to use the startup program to input the name, then be able to use the name later in the main program.
Upvotes: 0
Views: 82
Reputation: 4419
I think is better do your code in classes and functions. I suggest you to do:
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()
Upvotes: 0
Reputation:
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.
Upvotes: 0