Skip to content
Advertisement

limited input numbers and save a list into a list

out = []
for a in range(1):
    for i in range(1):
        while True:
            grid = input("input: ")
            if grid.isnumeric() and int(grid) >=0 and int(grid) <10:
                out.append(grid)
                break
            else:
                print("Invalide Input")
print(*out)

user input example

input:1 2 3 4 5 6 7 8 9

split using ” ” the space between them and turn them to inteager the and should be =

grid =  [[1,2,3,4,5,6,7,8,9 ]]...

but after spliting and converting each number should be < 10 and >= 0

and save it to be the

grid = [[1,2,3,4,5,6,7,8,9 ]]

i need to input the line 9 times not once and been duplicate

Advertisement

Answer

Here’s the code updated for your current question.

out=[]
count = 0
while(count < 9):
    user_input = input("input: ")
    grid = user_input.split(" ")
    inputs = True
    while(inputs):
        if len(user_input) == 17:
            if len(grid) == 9:
                try:
                    grid = [int(a) for a in grid]
                    inputs= False
                except:
                    print('you inputed a letter')
                    user_input = input('input: ')
                    grid = user_input.split(' ')
            else:
                print('your input does not include 9 numbers.')
                user_input = input('input: ')
                grid = user_input.split(' ')
        else:
            print('your input is not of proper length,')
            user_input = input('input: ')
            grid = user_input.split(' ')
    out.append(grid)
    count += 1
print(out)

When I enter the numbers 1 through 9 nine times, it outputs the following,

[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9]]

If I enter a negative number or a number with more than 1 digit, s will be the wrong length, or l will not contain enough numbers. Each of your inputs are checked properly and out becomes a list of 9 lists containing nine integers. Now it prompts for that original input 9 times, providing a list of 9 separate lists with numbers chosen by the user.

Advertisement