I am trying to receive user input and then put it into a list. I have a working version, however, I feel as though it is redundant(the first block seems redundant, though if I take it away, it doesn’t work). Any way this can be improved?
Thanks
def pastChoice():
choice = raw_input("> ")
prevMove = []
prevMove.append(choice)
for i in prevMove:
choice = raw_input("> ")
prevMove.append(choice)
print prevMove
pastChoice()
I am making a game and I want to show the user what choices they have already made.
Ex. Type ‘history’ if you would like to see previous choices
[‘a’,’a’,’c’,’b’]
I’m not sure about your intentions for this code, but this is roughly equivalent:
the block:
asks user for input and appends it to prevMove, so at loop start, the condition
i in prevMoveevaluatesito the first element inprevMove, then because in the second block you’re appending new elements toprevMove, the iteration continues over those the newly appended elements and the loop never ends.If you remove the first block, but keep the
forloop, it won’t work because there are no elements insideprevMoveand therefore nothing to iterate over.