Search code examples
pythonlistpython-2.7raw-input

list not entering first input


I am noob and I am working on this python project and i cant get the first input entered by the user in a array my code. Thanks in advance Here is my code:

def ask():
    user_input = raw_input("Enter a number: ")
    user_input_array = []
    count = 0
    quits = 'done'

    while user_input != quits:
        user_input = raw_input("Enter a number: ")
        try:
            if type(user_input) == str:
                num = int(user_input)
                user_input_array.append(num)
                count = count + 1

        except:
            print("Invalid input")

    while user_input == quits:
        #user_input_array.remove('done')
        print ("done")
        print ('Count: ', count)
        print (user_input_array)
        break
ask()

Solution

  • That is because you never put it in there.

    def ask():
        user_input = raw_input("Enter a number: ")
        user_input_array = [user_input] # Create the list with the original input
        ...
    

    With the above, the first thing entered by the user is placed in the list when the list is created. You might want to do your checks before this