-4

I was trying to run a program within another program using the import feature. It runs, however, it bypasses my function and just runs the other program.

import ex35
def beginning():
    print "you have three door choices.  door a,b,c"
    door=raw_input("> ")
    if door=="c":
        ex35.start(door)
    elif door=="a":
        print "do i have to put stuff down?"
    else:
        print "i finally got it to work"

beginning()

if __name__ == "__main__":
   # stuff only to run when not called via 'import' here
   main()
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • 2
    what's in ex35? – TallChuck Aug 07 '17 at 22:11
  • If you have code that will be run normally in `ex35` put that into a `if __name__ == "__main__":` loop as when you import a python file it runs as described in [this](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) post. – Professor_Joykill Aug 07 '17 at 22:13
  • 1
    Possible duplicate of [Why is Python running my module when I import it, and how do I stop it?](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) – Professor_Joykill Aug 07 '17 at 22:13
  • the program ex35 has three options. in other words the game i am making is huge. it starts off asking about three doors (one of which leads to ex35) and in ex 35 it askes the user for three more choices. – Daniel Anderson Aug 07 '17 at 22:19
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Aug 07 '17 at 22:29
  • the if name loop doesnt work. which i dont understand why. – Daniel Anderson Aug 07 '17 at 22:33
  • You should read this: https://stackoverflow.com/questions/4041238/why-use-def-main and this https://stackoverflow.com/questions/419163/what-does-if-name-main-do – Landmaster Aug 08 '17 at 00:17

1 Answers1

0

okay i figured it out. kinda sorta of . the whole if_name_=="main: didnt work so i came up with this

def start(): print "you have three door choices. door a,b,c" door=raw_input("> ")

if the user puts c then it should run ex 35 function start

if door=="c":
     import ex35
elif door=="a":
    import ex1
else:
    import ex2

start()