user11679142
user11679142

Reputation:

Looping through a list in Python does not return the expected True or False value

I am creating a Bingo console application in Python and am having a problem with (possibly) the remaining_selections() function.

The idea is when a user guesses a number in the list, that number gets replaced with an 'X' (single digit) or 'XX' (double digit).

After each user input, the list is checked to see if any there are any integers remaining in the list and if not the user has a BINGO.

The problem I'm facing is that the return value of the remaining_selections() function always returns True regardless of the logic I use.

The reason I suspect the problem is in the remaining_selections() function is because when I hard code a False value the code executes a 'Bingo!!'

Any ideas where I am going wrong??

It's not a large program so have included the entire code.

def draw_bingo_card():
    '''
    Draw bingo card from bingo_card list
    '''

    print(
        f"\n",
        f"----------------\n",
        f"   BINGO CARD\n",
        f"----------------\n",
        f"| {bingo_card[0]}|{bingo_card[1]}|{bingo_card[2]}|{bingo_card[3]}|{bingo_card[4]}|\n",
        f"----------------\n",
        f"|{bingo_card[5]}|{bingo_card[6]}|{bingo_card[7]}|{bingo_card[8]}|{bingo_card[9]}|\n",
        f"----------------\n",
        f"\n",
    )


def remove_number(number):
    '''
    Check if the number chosen matches a number in the list
    If a number matches, change the value to an X
    ''' 
    for i, item in enumerate(bingo_card):
        if item == number:
            if number < 10:
                bingo_card[i] = "X"
            else:
                bingo_card[i] = "XX"


def remaining_selections():
    '''
    Check the bingo_card list and if any values 
    are not X or XX return True (indicating that 
    there are remaining selections)
    '''
    integers_exist = False

    for item in bingo_card:
        if item != "X" or item != "XX": 
            integers_exist = True
    return integers_exist




# Bingo Card List
bingo_card = [7,26,40,58,73,14,22,34,55,68]

# Draw Bingo Card to Terminal
draw_bingo_card()


while True:
    try:
        user_input = int(input("Please enter a number between 1 and 80 inclusive: "))

        #####################
        # DEBUGING STATEMENT
        if user_input == 0:
            print(bingo_card)
        #####################

        # Check to see if user has entered a valid integer range
        if user_input < 1 or user_input > 80:
            print("ERROR: Numbers betwen 1 and 80 are only valid")
            continue
        else:
            remove_number(user_input)
            draw_bingo_card()

            if remaining_selections():
                continue
            else:
                print(
                    "\n",
                    "***********\n",
                    "* BINGO!! *\n",
                    "***********\n"
                )
                break

    except:
        # User has entered an invalid selection
        print("ERROR: Invalid selection.  Please choose an INTEGER VALUE between 1 and 80 inclusive")
        continue

Upvotes: 0

Views: 78

Answers (1)

luigibertaco
luigibertaco

Reputation: 1132

def remaining_selections():
    '''
    Check the bingo_card list and if any values 
    are not X or XX return True (indicating that 
    there are remaining selections)
    '''
    integers_exist = False

    for item in bingo_card:
        if item != "X" and item != "XX":  # it should be `and` instead of `or`
            integers_exist = True
    return integers_exist

you could simplify it even more with:

if item not in ["X", "XX"]:
    ...

Upvotes: 1

Related Questions