0

I have the following code:

while True:
    line = raw_input('Enter number:')
    try:
        if line == 'done':
            break
        if int(line) == ():
            continue
    except:
        print 'invalid input'
print 'Done!'
print line

I would like to create a list of the numbers that are entered by the user. Can some help?

3
  • You should try to do it on your own first and then post that code here in the question. Then, you should ask a question about the code that you wrote. Commented Mar 18, 2016 at 17:47
  • You're almost there. What erroneous behavior do you notice? Commented Mar 18, 2016 at 17:48
  • I am a novice coder. Only started learning python a couple of days ago. Sorry if I am incorrectly using stackoverflow - I'm just keen to learn and develop my skills Commented Mar 18, 2016 at 17:57

2 Answers 2

1
number_list = []
while True:
    line = raw_input('Enter number:')
    if line == 'done':
        break
    try:
        n = int(line)
        number_list.append(n)
    except ValueError:
        print 'invalid input'
print 'Done!'
print number_list
Sign up to request clarification or add additional context in comments.

1 Comment

@HemeshPatel fell free to accept this answer if it solved your problem: meta.stackexchange.com/a/5235
0

Just one more variant:

res = []
line = raw_input('Enter number:')
while line.lower() != 'done':
    try:
        res.append(int(line))
    except ValueError: 
        print 'Invalid input'
    line = raw_input('Enter number:')
print 'Done!'
print " ".join(map(str, res))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.