0
votes

I'm new to Python v3 and am using a while loop at the end of my program to determine if the user wants to restart/retry the program or finish.

If I select yes and I repeat the program more than once and then select no, I keep returning the "Would you like to search again: (Y/N) > " option for the number of times I re-tried the program e.g. 3 attempts and I have to enter n three times before break takes effect.

Code used is below.

while True:
    finish_input = input("Would you like to search again: (Y/N) > ")            
    if finish_input.lower() == ("y"):
        my_project()#restarts the program from the start
        continue
    elif finish_input.lower() == "n":
        print()
        print("Thank you for using this service.")
        break
    else:
        print()
        print("Invalid entry.  Please enter Y or N")

I want the option to restart but only have to input n once to close/break the program and exit. Help would be really appreciated.

2

2 Answers

0
votes

What you want is:

def my_project():
    #Your other code here
my_project()
#The code that you posted

But you are doing:

def my_project():
    #Your other code here
    #The code that you posted

The difference is that in the last one, you are looping inside the program: each y is another call to the whole function that later and for each one of those you'll have to put n.

The code would look like this:

def my_project():
    #Your other code here
my_project()

while True:
    finish_input = input("Would you like to search again: (Y/N) > ")
    if finish_input.lower() == "y": my_project()
    elif finish_input.lower() == "n":
        print("\nThank you for using this service.")
        break
    else: print("\nInvalid entry.  Please enter Y or N")
0
votes

I think its a bad way to implement this. How about doing something like this.

#program starts
run_prog = True

while run_prog:
  #Your original code

  finish_input = "a"
  while True:
    finish_input = input("Would you like to search again: (Y/N) > ") 
    if finish_input.lower() == ("y"):
        run_prog = True
        break
    elif finish_input.lower() == "n":
        run_prog = False
        print()
        print("Thank you for using this service.")
        break
    else:
        print()
        print("Invalid entry.  Please enter Y or N")