[Python] Except message not being displayed [closed]

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    The except message is not displaying the print message even though the index is out of range.

    I've done if not in range in the try block to get print("index out of range") but that defeats the purpose of the except handling. I was expecting the except block to print("index out of range")

    ages = [3, 12, 45, 17, 81]
    index_choice = int(input("select a number"))
    try:
    if index_choice in range(len(ages)):
    print(ages[index_choice])
    except IndexError:
    print("index out of range")

    Continue reading...

Compartilhe esta Página