If list index exists, do X

前端 未结 12 878
清酒与你
清酒与你 2020-12-02 11:41

In my program, user inputs number n, and then inputs n number of strings, which get stored in a list.

I need to code such that if a certain

12条回答
  •  鱼传尺愫
    2020-12-02 12:22

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

    You already know how to test for this and in fact are already performing such tests in your code.

    The valid indices for a list of length n are 0 through n-1 inclusive.

    Thus, a list has an index i if and only if the length of the list is at least i + 1.

提交回复
热议问题