1

I'm attempting to build a code which creates a list of 9 lists, each also holding 9 elements. I've created a converter to validate the size of each entry, then splits them into the individual elements of its separate. It properly notices incomplete lengths and requires the proper number of valid inputs before outputting, but for some reason each index that had read invalid the first time around will not return the new, valid input. Below is the code.

Is it just some simple syntax that i'm overlooking or is my code not sufficient enough?


print('Please enter your line with no spaces or commas')

board_list = [[], [], [], [], [], [], [], [], []]

def convert(line):
    li = [int(x) for x in str(line)]
    if len(li) == 9:
        return li
    else:
        print("Your line was not valid, please try again.")
        convert(input())

def create_board(bo):
    for row in range(len(bo)):
        print('Please enter the line below:')
        board_list[row] = convert(input())

create_board(board_list)
print(board_list)

'''
vebs
  • 11
  • 1
  • Which version of python are you using? Most of all: your code isn't pythonic. Avoid unnecessary indexes – Pynchia Jun 13 '20 at 04:56

3 Answers3

1

Add the return in else statement in convert() method

def convert(line):
    li = [int(x) for x in str(line)]
    if len(li) == 9:
        return li
    else:
        print("Your line was not valid, please try again.")
        return convert(input())
deadshot
  • 8,881
  • 4
  • 20
  • 39
1

In the last line of convert, you need to return the value. That is,

return convert(input())

As it is, you're asking the user for input, running that through convert, but then not doing anything with the result.

EDIT:

As a side note, I would recommend against this recursive call to convert. It's not likely but if the user keeps giving incorrect input, you'll run into a recursion depth error. It's better to do the validation of the input outside of convert.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
0
print('Please enter your line with no spaces or commas')

board_list = [[], [], [], [], [], [], [], [], []]

def convert(line):
    li = [int(x) for x in str(line)]
    if len(li) == 9:
        return li
    else:
        print("Your line was not valid, please try again.")
        return convert(input())

def create_board(bo):
    for row in range(len(bo)):
        print('Please enter the line below:')
        board_list[row] = convert(input())

create_board(board_list)
print(board_list)
Atharva Kadlag
  • 302
  • 3
  • 15