-1

User input should be a list of nine integers. The code snippet below achieves that but it requires nine inputs from the user.

Is there a way to allow the user to just enter nine integers in a row and press enter?

row=[]

for i in range(0,9):
    while True:
        try:
            number = int(input(f"row 1, col {i+1}: "))
            if 0 <= number <= 9:
                row.append(number)
                break
            raise ValueError()
        except ValueError:
            print("Input must be an int between 0 and 9.")

Henri
  • 1,077
  • 10
  • 24

1 Answers1

2

Try something like:

user_input: list = list(map(int, input().split()))

We firstly grab an input(), eg. "1 2 3 4 5", then we split() it and we have ["1", "2", "3", "4", "5"], finally we map them into integers and convert to a list :)

Jakub Bednarski
  • 261
  • 3
  • 9