I have an interact() function which I need to ask for a file. I already have a function load_maze(filename) which loads my text document in python. How do i get interact() to ask for my filename in load_maze(filename)?
            Asked
            
        
        
            Active
            
        
            Viewed 3,893 times
        
    -2
            
            
        - 
                    2How does your program, uh, interact with the user? – Ignacio Vazquez-Abrams Apr 01 '12 at 04:52
- 
                    Is this commandline or graphical if so which framework are you using? – jamylak Apr 01 '12 at 04:54
- 
                    I think you need to provide a little bit more information on what your problem is. It sounds like interact is a custom function, if so you need to let everyone know what it is supposed to do. Also letting everyone know what you have attempted will be a great help. If you are simply trying to get user input from the command line then you can look at this question http://stackoverflow.com/questions/70797/python-and-user-input – rhololkeolke Apr 01 '12 at 04:56
- 
                    @ ignacio When I run interact(), the user has to type the filename they want to load. I'll try to be more precise. e.g raw_input("Maze File:") maze1.txt. I want the user to define a text document i have created. – Anthony Do Apr 01 '12 at 05:00
- 
                    Could you show us the existing code of `interact()`? Could you show the code that arranges for `interact()` to get called? – Karl Knechtel Apr 01 '12 at 07:01
2 Answers
2
            
            
        That depends on your program. The easiest interaction I could imagine is asking for it in the console. Use raw_input.
response = raw_input('give me a file name')
if you are using py3k beware because raw_input() is now input(). input() is also in py2k but there it has a different behavior than raw_input.
 
    
    
        joaquin
        
- 82,968
- 29
- 138
- 152
- 
                    @ joaquin If i use that code, how do i get python to open the response (in your example) – Anthony Do Apr 01 '12 at 05:07
- 
                    
- 
                    @AnthonyDo I am not sure what do you mean. `response` is the name of a variable that holds wathever you entered in the console. Then you should use your function: `load_maze(response)` – joaquin Apr 01 '12 at 05:13
2
            Anthony, maybe something like this?:
#!/usr/bin/python
def interact():
    fn = raw_input("Enter a filename: ")
    return fn
def load_maze(fn):
    myfile = open(fn)
    maze_txt = myfile.read()
    myfile.close()
    return maze_txt
print load_maze(interact())
 
    
    
        uvtc
        
- 710
- 3
- 8
