[Python] If list index exists, do X

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    I need to code such that if a certain list index exists, then run a function.

    My failing code:

    n = input ("Define number of actors: ")
    count = 0
    nams = []
    while count < n:
    count = count + 1
    print "Define name for actor ", count, ":"
    nams.append(raw_input())
    if nams[2]: # 'if nams[2] exists, do something depending on len(nams)
    if len(nams) > 3:
    do_something

    Continue reading...

Compartilhe esta Página