Python Forum
How to skip to a different line while importing a different script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to skip to a different line while importing a different script
#1
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 = False
and 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!
Reply
#2
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*")
Reply
#3
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)
Output:
In main 1 In breakin 2 In main 3 In breakin 4 In main 5 Leaving main 5 Leaving breakin 4 Leaving main 3 Leaving breakin 2 Leaving 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help, a script line is skipped kucingkembar 5 1,391 Dec-29-2021, 07:34 PM
Last Post: deanhystad
  need to skip password prompt, continue... tester_V 2 1,437 Oct-19-2021, 05:49 PM
Last Post: tester_V
  Delimiters - How to skip some html tags from being translate Melcu54 0 1,621 May-26-2021, 06:21 AM
Last Post: Melcu54
  How to skip a folder directory in a loop mfkzolo 2 12,365 Nov-18-2020, 07:56 AM
Last Post: mfkzolo
  read the first line in my script chubbychub 1 1,698 Nov-09-2020, 03:18 AM
Last Post: Larz60+
  How to skip LinkedIn signup link using python script? Mangesh121 0 1,766 Aug-26-2020, 01:22 PM
Last Post: Mangesh121
  importing a list of numbers into python script barrypyth 8 4,437 Aug-22-2020, 09:10 PM
Last Post: barrypyth
  How to calculate column mean and row skip non numeric and na Mekala 5 4,835 May-06-2020, 10:52 AM
Last Post: anbu23
  Functions, skip an arugment SpongeB0B 2 2,058 Mar-27-2020, 12:10 PM
Last Post: SpongeB0B
  Skip a line doug2019 4 3,118 Oct-09-2019, 06:56 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020