[Tutor] Enter mode where you can input list indices

Julius Hamilton juliushamilton100 at gmail.com
Sat Oct 16 11:20:10 EDT 2021


Hey,

I would like to explore the contents of a list where I could just enter a
mode where any index I type in, it would print the value for some list,
like this:

>>> p()
1
["apple", "cat"]
1 2
cat
3
Index out of bounds

>>>

It will be necessary to loop until the input is nothing:

def p():

  n = input()

  while n != "":

After the input is received it must be parsed into a list of integers:
    n = n.split(' ')
    indices = []
      for item in n:
        indices.append(int(item))

This converts a sequence of integer characters to a list of integer
indices. Next, index the list (in the enclosing scope) by however many
indices there are:

    for i in indices:
      list = list[i]

    # then start again
    n = input()

There are a few questions to straighten out here which I will research and
think about but if anybody could help convert this pseudocode to effective
code and show me the most elegant way they'd achieve this I'd really
appreciate it.

Thanks very much,
Julius


More information about the Tutor mailing list