1

I am writing a python program in which I want to allow the user to enter only 10 numbers from the terminal. If they try to enter more than 10 it should prompt that they can't enter more than 10 numbers. How can I achieve this in python?

My code looks like as follows:

def get_list():
    '''takes data from the user to create a list'''

    error =True

    while error == True:
        error =False
        user_data = raw_input("Please enter integers separated by space:" ) 
        listofdata =user_data.split()
        for item in listofdata:
            try:
                item = int(item)
            except:
                print("Please enter only numbers separated by spaces")
                error =True
                break
    return listofdata
1
  • 1
    Please fix your code indentation. In Python, indentation is tantamount to the program's structure, so it is impossible to debug if not indented correctly. Notably, your return statement is at the top level of this sample. If that is accurate, then it would necessarily be part of some enclosing scope that is not even shown here. Commented Oct 19, 2015 at 3:51

1 Answer 1

1

After the line:

listofdata =user_data.split()

you can add a check, something like:

if len(listofdata) > 10:
    print "You should enter 10 integers only, please try again!"
    error = True
    continue
Sign up to request clarification or add additional context in comments.

1 Comment

@akira, Please accept sufficient answers with the checkmark button. It's worth +2 rep to you.

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.