JavaScript
x
12
12
1
out = []
2
for a in range(1):
3
for i in range(1):
4
while True:
5
grid = input("input: ")
6
if grid.isnumeric() and int(grid) >=0 and int(grid) <10:
7
out.append(grid)
8
break
9
else:
10
print("Invalide Input")
11
print(*out)
12
user input example
JavaScript
1
2
1
input:1 2 3 4 5 6 7 8 9
2
split using ” ” the space between them and turn them to inteager the and should be =
JavaScript
1
2
1
grid = [[1,2,3,4,5,6,7,8,9 ]]
2
but after spliting and converting each number should be < 10 and >= 0
and save it to be the
JavaScript
1
2
1
grid = [[1,2,3,4,5,6,7,8,9 ]]
2
i need to input the line 9 times not once and been duplicate
Advertisement
Answer
Here’s the code updated for your current question.
JavaScript
1
28
28
1
out=[]
2
count = 0
3
while(count < 9):
4
user_input = input("input: ")
5
grid = user_input.split(" ")
6
inputs = True
7
while(inputs):
8
if len(user_input) == 17:
9
if len(grid) == 9:
10
try:
11
grid = [int(a) for a in grid]
12
inputs= False
13
except:
14
print('you inputed a letter')
15
user_input = input('input: ')
16
grid = user_input.split(' ')
17
else:
18
print('your input does not include 9 numbers.')
19
user_input = input('input: ')
20
grid = user_input.split(' ')
21
else:
22
print('your input is not of proper length,')
23
user_input = input('input: ')
24
grid = user_input.split(' ')
25
out.append(grid)
26
count += 1
27
print(out)
28
When I enter the numbers 1 through 9 nine times, it outputs the following,
JavaScript
1
2
1
[[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]]
2
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.