so I am making a simple program in python (which I am very new to), and it is a simple menu with 4 options. One of the options is used to display the users from a text file, and then it will ask the user if they want to return to the main menu. If they enter yes, my intention is to return the user to the main menu and continue from there, and if no, then it will just be passed. The problem is, when the user inputs "Y" to return back to the main menu, it will take the user there, but the same "go back" input question will be asked continuously in the loop. How do I break this? I am trying to make it so that when the user selects "Y" for go back, it will take them to the main menu, and ask the user which option they want to pick again.
Side note: If possible, it would mean a lot if someone could also tell me how to add drop down lines, so that, my program that reads a text file of usernames will then display each individual user as a drop down list in the program.
Any help would be much appreciated, thanks!
Code:
elif option == 4:
print("\n" "Displaying users... ")
userlist = open('userlist.txt', 'r')
f = userlist.readlines()
newList = []
for line in f:
if line[-1] == '\n':
newList.append(line[:-1])
else:
newList.append(line)
print(newList)
while True:
goback = input("Go back? (Y/N): ")
if goback == "Y":
menu()
elif goback == "N":
pass
else:
print ("Invalid option")
The area I am struggling with is:
goback = input("Go back? (Y/N): ")
if goback == "Y":
menu()
as I want it to take me back to the main menu entirely instead of asking the same question over and over.
I have tried to get it to display the main menu without the input, but I am not really too sure how to do this. I have addded menu() to take the user back to the main menu, where they are able to select an option again. I have tried following up menu() with my prompt question option = int(input("Enter your option: "))
but did not have any success, as when I entered an option, it went back to the same "go back?" question
CodePudding user response:
Solution is already there in comments so, code:
while True:
goback = input("Go back? (Y/N): ")
if goback == "Y":
menu()
break
elif goback == "N":
pass # Write code according to the action
else:
print ("Invalid option")
Output:
Displaying users...
Go back? (Y/N):
N
Go back? (Y/N):
N
Go back? (Y/N):
N
Go back? (Y/N):
Y
Ends here because it is just a snippet