[Solved] How to make items on a list show, with input from the user [closed]


You can do that:

a = ["first answer", "second answer", "last answer"]
answer = raw_input("\n".join([chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]))

Edit (for Python 3):

a = ["first answer", "second answer", "last answer"]
answer = input("\n".join([chr(x + ord('A')) + ") " + a[x] for x in range(len(a))]))

Explanation:

for x in xrange(len(a))

Iterates through the indexes of the list a

chr(x + ord('A'))

Converts the indexes into letters – 0->A, 1->B, 2->C

chr(x + ord('A')) + ") " + a[x]

Formats a string containing the answer letter and the answer itself.

A) first answer
B) second answer
C) last answer

Each one seperatedly.

[chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]

Generates a list of formatted answer strings (iterates through a, formats a string, and dumps it into a list.

Now,

"\n".join[chr(x + ord('A')) + ") " + a[x] for x in xrange(len(a))]

Takes that list, and concatenates it with \ns. Now everything left is to print it and wait for an answer.

Hope I helped.

10

solved How to make items on a list show, with input from the user [closed]