Question

I'm new in python and have not much experience in programming also.

I have this code in my main file:

def user_input():
   import re
   name_check = re.compile(r"[^A-Za-z\s. ]")
   print("Please enter name input: ")
   name = raw_input("> ")
   return name

def test():



if __name__ =="__main__":
   user_input()
   test()

How can I get the user_input parameter in order to process it in others child modules? I'm trying to import the main module to the child file module but it doesn't work. This is my program structure:

/main
  __init__.py  
  main.py
  /child
    __init__.py    
    child1.py
    child2.py

I need to pass user_input data to child1.py. When I do importing main:

from main.main import user_input

I've got error message:

No module named main.main

Any comments would be much appreciate.

Cheers

Was it helpful?

Solution

This code takes in input through raw_input() in the user_input() function, and then uses return to return it. In our print_input() function, we take in the input. In our main() function, we assign the return value from user_input() to name, and then pass it into print_input().

def user_input():
    name = raw_input("Please enter name input:\n> "
    return name

def print_input(name):
    print "Hello", name

def main():
    name = user_input()
    print_input(name)

if __name__ =="__main__":
    main()

Edit

main.py:

def user_input():
    name = raw_input("Please enter name input:\n> "
    return name

def print_input(name):
    print "Hello", name

def main():
    name = user_input()
    print_input(name)

To call a function from here do the following:

>>> from main import user_input, print_input
>>> 
>>> print_input(user_input())
Please enter name input:
> aj8uppal
Hello aj8uppal
>>> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top