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 and also stops bringing up the names for user 1 to input their response... it just repeats the user input field "y/n: ". I know how to use the while true if it is just true or not... but I'm not sure how to use the while True, if the user input can be either 'y' or 'n'.

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
  • 1
    The indentation in your code is all messed up and and a large part of it seems to be missing entirely so it's hard to debug -- but take one of the standard solutions, put it in its own function (just get it working on its own *first*), and have your code call that for each user. – Samwise Aug 29 '21 at 15:47
  • That's just the way it pasted into the input field. The indentation is working fine. It just pasted over the global over to the left handside and so moved everything else over with it. If you imagine the def boys(): is over to the left it should all be correct. – edders79 Aug 29 '21 at 15:59

0 Answers0