Search code examples
pythonpython-2.7user-input

Python2.7-: Storing user input values input in a list


I am new to python and came around a scenario explained below-:

This is one from the .pdf I am referring to learn. Would be great if anyone could guide or share some other resources.

A program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.

Enter a number: 4
Enter a number: 5
Enter a number: bad data
Invalid input
Enter a number: 7
Enter a number: done

16 3 5.333333333333333*

I am unable to store the values into list.

Tried going with this logic-:

while True:
    line = input('Enter Number-: ')
    if type(line) == int():
        continue
    if line == 'done':
        break
    print(line)
print('Done!')

Just need to know how to store into lists without using spaces or commas, The user should be able to enter the value as shown in example above and those should get stored in a list.

Thanks in advance.


Solution

  • In Python 2.7, input will evalulate any entry and will fail if the input is not a correct Python type to begin with. It's better to use raw_input here as any entry will be considered a string. If you move to Python 3, raw_input was removed and input acts how raw_input did. So your example expects you to give it '45' or 'done' instead of 45 or done.

    But the reason you're unable to store any values into a list is because you're not adding them to a list in the first place. But since we've also switched to raw_input, we don't know if the entry is a valid number or not. So we need to try to convert it to a number and if it isn't one, then check to see if it's the keyword telling the code to stop.

    values = []  # make an empty list
    while True:
        line = raw_input('Enter Number-: ')  # all entries here are considered strings
        try:
            num = int(line)  # convert to an integer
            values.append(num)  # add to list
            continue  # return to input query
        except:  # int(line) threw an error, so not a valid number input
            if line == 'done':  # check if should stop
                break  # get out of loop
            else:  # anything else
                print 'bad data, invalid input'
                continue  # return to input query
    
    print 'Done!\n'     
    print 'total:', sum(values)
    print 'count:', len(values)
    print 'average:', sum(values) / float(len(values))
    

    If you're entering more than just integers, you may wish to change num = int(line) to num = float(line) to handle decimal inputs, as int only accepts integers.

    Enter Number-: 4
    Enter Number-: 5
    Enter Number-: 
    bad data, invalid input
    Enter Number-: 7
    Enter Number-: done
    Done!
    
    total: 16
    count: 3
    average: 5.33333333333
    

    The Tutorial may also be helpful in learning Python.