tags:

views:

78

answers:

2

when using raw_input in a loop, until a certain character is typed (say 'a') how can I print all the inputs before that, in reverse order, without storing the inputs in a data structure?

using a string is simple:

def foo():

    x = raw_input("Enter character: ")
    string = ""
    while not (str(x) == "a"):
        string = str(x) + "\n" + string
        x = raw_input("Enter character: ")
    print string.strip()

but how could I without the string? any ideas?

+1  A: 

You have to store the results in some data structure. Instead of a string, however, you could store each input in a list and avoid all the string concatenation:

l = []
x = raw_input("Enter character: ")
while not (str(x) == 'a'):
    l.append(x)
    x = raw_input("Enter character: ")

print '\n'.join(l[::-1])
Connor M
+4  A: 

This is not a practical approach, but since you asked for it:

def getchar():
    char = raw_input("Enter character: ")
    if char != 'a':
        getchar()
        print char

getchar()

Of course this only means that I'm using "hidden" data structures, the local namespace and the call stack.

THC4k
+1 for needlessly complicated
Falmarri