![]() |
How to skip to a different line while importing a different script - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to skip to a different line while importing a different script (/thread-32321.html) |
How to skip to a different line while importing a different script - kat_gamer - Feb-03-2021 Hi! I was coding a program as a little side project that I work on in my spare time and I found a sort of, unwanted occurrence in my program. I want the code to go to the line after I imported another file, and instead, it sends me to the beginning of the file. Here's the code (main.py to be precise): print("Good evening ladies,gentlemen and all other configurations of being! I'm Alastor, the radio demon!") print("I have called you here because of your impeccable skills as a thief") ans = input("SO, will you join me on this quest of epic proportions?") if ans == "no": print("Ahaha, this isn't a choice my friend, I shall hold this over your head for later!") stubborn = True else: print("Alright! Let's get to work") stubborn = False import choice print("Great! the guards left should we go in now") if stubborn: print("I don't need to ask do I, we're heading in anyways") else: ans = input("So, should we go inside?") if ans == "yes" or "Yes": print("Yes! Alright, we're heading in.") indecisive = False assertive = False elif ans == "no" or "No": print("Ahaha,too late my friend, i'm already walking in. ") indecisive = False assertive = True else: print("Indecisive eh? I'll keep that in mind") indecisive = True assertive = Falseand here's the second file's code (choice.py): def breakin(): print() print("Ok! We're here") br = input("How will we get inside sneak in: bust in or wait!") if br == "sneak in": print ("*You snuck in at night from the ceiling*") elif br == "bust in": print("I was hoping you'd say that lets go!") elif br == "wait": print("*you waited for the guards to leave*") import main breakin()And it starts from the beginning. Thanks! RE: How to skip to a different line while importing a different script - BashBedlam - Feb-03-2021 You can remove the last two lines of choice.py . You most likely don't want to call breakin () right when you import it and you never have to import the file that your are currently running. After that just add choice.breakin () where ever in the code that you would like that function to start. Here's an example :main.py import choice print("Good evening ladies,gentlemen and all other configurations of being! I'm Alastor, the radio demon!") print("I have called you here because of your impeccable skills as a thief") ans = input("SO, will you join me on this quest of epic proportions?") if ans == "no": print("Ahaha, this isn't a choice my friend, I shall hold this over your head for later!") stubborn = True else: print("Alright! Let's get to work") stubborn = False print("Great! the guards left should we go in now") if stubborn: print("I don't need to ask do I, we're heading in anyways") else: ans = input("So, should we go inside?") if ans == "yes" or "Yes": print("Yes! Alright, we're heading in.") choice.breakin () indecisive = False assertive = False elif ans == "no" or "No": print("Ahaha,too late my friend, i'm already walking in. ") indecisive = False assertive = True else: print("Indecisive eh? I'll keep that in mind") indecisive = True assertive = False choice.py def breakin(): print() print("Ok! We're here") br = input("How will we get inside sneak in: bust in or wait!") if br == "sneak in": print ("*You snuck in at night from the ceiling*") elif br == "bust in": print("I was hoping you'd say that lets go!") elif br == "wait": print("*you waited for the guards to leave*") RE: How to skip to a different line while importing a different script - deanhystad - Feb-03-2021 You have a recursive script. main calls breakin which calls main which calls breakin and so on and so on... Normally this kind of thing is done with function calls, but you are doing it with import statements. The reason things don't take off where they left off in main is because you never return to main. Instead you call a new main. The original main is still hanging out waiting for breakin to be done, but breakin never finishes. This may be easier to see if you use functions. def main(index): print('In main', index) if index < 5: breakin(index+1) print('Leaving main', index) def breakin(index): print('In breakin', index) main(index + 1) print('Leaving breakin', index) main(1) As you can see by the printout, calling main from breakin does not return to main. It starts a brand new main. The old main is still there, waiting. In this example I use an if statement to end the recursion so main is finally allowed to finish. This is when we finally start seeing the code that is after the recursive call.
|