Home > Back-end >  How to loop a list until all items in a list are matched with user input then stop the loop with Bin
How to loop a list until all items in a list are matched with user input then stop the loop with Bin

Time:12-23

my_list items should be matched with user input when matched all numbers a message should show bingo and loop should be stopped

print('...............Welcome to BINGO.................')

my_list = ["15", "22", "35", "48", "80", "55", "12", "36", "45", "26"]
    
i = str(input("press ENTER to play BiNgO!"))
     
for i in range(10):
    number_input = input("enter a number between 1 to 80")

    if number_input in my_list:           
        my_list.remove(number_input)
        print("hurray! this number is matched")
    else:
        print("oops! not matched")

CodePudding user response:

Try this:

my_list = ["15", "22", "35", "48", "80", "55", "12", "36", "45", "26"]

i = str(input("press ENTER to play BiNgO!"))

for i in range(10):

   number_input = input("enter a number between 1 to 80")

   if number_input in my_list:
       my_list.remove(number_input)
       print("hurray! this number is matched")
       break

   else:
       print("oops! not matched")

CodePudding user response:

We need to use while loop when we don't know the number of iterations to be made.

The while loop should terminate when my_list is empty.

So the code should be:

print('...............Welcome to BINGO.................')

my_list = ["15", "22", "35", "48", "80", "55", "12", "36", "45", "26"]
    
i = input("press ENTER to play BiNgO!")
     
while my_list != []:
    number_input = input("enter a number between 1 to 80")

    if number_input in my_list:           
        my_list.remove(number_input)
        print("hurray! this number is matched")
    else:
        print("oops! not matched")
  • Related