0

I'm new to coding and I have been trying to find a fix to this for days. I've tried a number of resources (including posts on here) but there isn't anything for my particular issue.

I'm trying to use a while True statement that is indented in a for loop. The for loop picks 20 random boy names from a list and prints to the terminal. I have a user input that asks for 'y' or 'n', and because user input is always TRUTHY I'm having trouble ensuring if the user inputs an invalid response that it is recognised as not TRUTHY (FALSEY). Hence it's making it hard to get the user to keep being presented the same name until they put a valid input in. I have tried changing the user input to falsey using the not method, but that still made the for loop break. Although I could have been coding it wrong. Below is a snip of the code, if anyone could advise how I get the while true to work (I think it needs to go after the boys_answer = input("y/n: ") line) it would be much appreciated.

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,

For example, below is one of the approaches I have tried, but this just stops the names being printed to the terminal after the first name appears. Can someone assist in helping with the answer to this please so that if the user inputs anything but 'y' or 'n' it won't let them proceed.

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)
        while True:
            try:
                boys_answer = input("y/n: ")
                boys_answer == 'y'
                boy_list_1.append(name)
                boys_answer = not boys_answer
            except ValueError:
                print("Sorry, bad input")
                continue

    user2_boy_start()
    return new_boy_list, boy_list_1,
edders79
  • 11
  • 3

0 Answers0