0

I've tried using the responses to similar questions on here e.g Asking the user for input until they give a valid response (Asking the user for input until they give a valid response). But that doesn't solve my problem, because it brings in bugs to my code further down the line with my user 2 loop.

Can someone advise what I need to do here please. The below function of mine is to iterate through a list of 20 random names. The user selects 'y' for yes and 'n' for no. If they select 'y' the name is appended to another list.

Nothing happens if 'n' is selected (or needs to I don't think because the 'y' is the only piece of data I need to do something with).

However... if I want the user to only be allowed to input 'y' or 'n', I'd like them to be told that they have entered an invalid entry and to try again. Thing is I need the most recent name to be repeated until they enter a valid input.

Some further info... there is a second user, who is then presented with the same names to also select their choices (hence the user 1 'y' inputs being appended to a list). The reason I say this is because I have tried a while True statement but I can't get it to work without messing up the user 2 choices (it seems to mess with any matches that the 2 users both select yes to). So ideally I need this function to not impact upon any further code later on. Any help please? I hope I've given enough detail and enough code for you to see. Basically, I think it needs to be a while True statement, but I can't get it to work. Thanks

def boys():
global boys_answer
global new_boy_list
print(f"{user1}, are you ready, your names are coming...")
for name in range(20):
    name = (random.choice(boy_names))
    boy_names.remove(name)
    print(name)
    new_boy_list.append(name)
    boys_answer = input("y/n: ")
    if boys_answer == 'y':
        boy_list_1.append(name)

user2_boy_start()
return new_boy_list, boy_list_1,
edders79
  • 11
  • 3
  • yeah, it should be a while code, but the condition is `input("y/n: ") not in ['y', 'n']` then it should ask input again like `input('invalid input, enter y/n: ')` which should go inside the while loop block – Ghost Ops Aug 29 '21 at 14:10
  • Thanks @GhostOps. For some reason this question has been closed and linked to another question, I saw that before I posted this but it doesn't solve my problem as it introduces bugs to my code. How can I 'unclose' this question? – edders79 Aug 29 '21 at 14:32
  • Try to edit the question and ask to reopen the question in the edit window (its a checkbox) – Ghost Ops Aug 29 '21 at 14:36

0 Answers0