簡體   English   中英

循環遍歷 Python 中的列表不會返回預期的 True 或 False 值

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

我正在 Python 中創建賓果游戲控制台應用程序,並且(可能)遇到了remaining_selections() function 的問題。

這個想法是當用戶猜測列表中的一個數字時,該數字將被替換為“X”(一位數)或“XX”(兩位數)。

在每個用戶輸入之后,檢查列表以查看列表中是否有任何整數,如果沒有,則用戶有一個 BINGO。

我面臨的問題是,無論我使用什么邏輯, remaining_selections() function 的返回值總是返回True

我懷疑問題出在 resting_selections remaining_selections() function 的原因是因為當我硬編碼一個False值時,代碼會執行一個“賓果游戲!!”

有什么想法我哪里出錯了嗎?

它不是一個大型程序,因此包含了整個代碼。

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
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

您可以通過以下方式進一步簡化它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM