Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add yes/no confirmation in python 3.X [closed]

I have a function that allows a user the ability to add data via input. I would like to add a confirmation step that will allow them to answer yes or no to continue. If they select no it should allow them to restart the function of adding data to the list. I also want to make sure they answer with Y, YES, y, yes, N, NO, n, no. What would be the best way to accomplish this? I've tried several solution I've found online but I end up not being able to get out of the loop of asking yes or no. Thanks in advance.

def item_list():  # Create a list 
    items = []
    item_int = 0
    while 1:
        item_int += 1
        item = input("\nEnter item %d or Press Enter: " % item_int)
        if item == "":
            break
        items.append(item)
    return items


items = item_list()
print(items)
like image 579
JHCTac Avatar asked Mar 09 '26 08:03

JHCTac


1 Answers

answer = input("Continue?")
if answer.lower() in ["y","yes"]:
     # Do stuff
else if answer.lower() in ["n","no"]:
     # Do other stuff
else:
     # Handle "wrong" input
like image 109
B. Plüster Avatar answered Mar 10 '26 20:03

B. Plüster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!