[Python] TypeError when trying to get input from user

Discussão em 'Python' iniciado por Stack, Maio 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to get the input of index from the user using the code below:

    menu = ["pasta", "pizza", "salad"]

    user_choice = input("Enter the index of the item: ")

    message = f"You chose {index} {menu[user_choice]}."
    print(message)


    The code produced the following error:

    Traceback (most recent call last):
    File "C:\Users\raiam\PycharmProjects\todo_app\bonus\bonus5.py", line 12, in <module>
    message = f"You chose {index} {menu[user_choice]}."
    ~~~~^^^^^^^^^^^^^
    TypeError: list indices must be integers or slices, not str


    I expect user choice from the menu through the item index in the list. How can I get that?

    Continue reading...

Compartilhe esta Página