Alantis123
Alantis123

Reputation: 21

Getting past a pylint test

When I do a pylint check on my code it says i have problems with my return statements. This is my code:

board = [['X', ' ', 'O'], ['X', 'O', ' '], ['X', ' ', ' ']]

def winner(board):
    """noughts and crosses game and checks if a row or column or diagonal is a winner"""
    row_ = []
    for row_number in range(3):
        row_.append(board[row_number])
    column_ = []
    for col_number in range(3):
        column_.append(list(board[0][col_number] + board[1][col_number] + board[2][col_number]))
    diagonal_0 = []
    diagonal_1 = []
    for selected_diagonal in range(2):
        if selected_diagonal == 0:
            diagonal_0.append(list(board[0][0] + board[1][1] + board[2][2]))
        diagonal_1.append(list(board[0][2] + board[1][1] + board[2][0]))
    total = row_ + column_ + diagonal_0 + diagonal_1
    for line in total:
        if line[0] == line[1] == line[2]:
            if line[0] == 'X':
                return 'X'
            elif line[0] == 'O':
                return 'O'
            elif line[0] == ' ':
                return None
            break

This is the error I get

************* Module source
R: 81, 0: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)

***Error***
Sorry, but your code doesn't pass the pylint style checks.

Upvotes: 1

Views: 234

Answers (2)

aahnik
aahnik

Reputation: 1861

# previous lines ...
for line in total:
        if line[0] == line[1] == line[2]:
            if line[0] == 'X':
                return 'X'
            elif line[0] == 'O':
                return 'O'
            elif line[0] and line[1] and line[2] != ' ':
                return line[0]
            return something
            break
        return something

This will work. Please try it.

break breaks out of a loop. Removing the else, will not affect anything.

Just do proper indentation of the return line[0]

Run the code and test it.

The second error will also be removed.

Upvotes: 1

Mahesh Anakali
Mahesh Anakali

Reputation: 344

The last part of the code is

else:
    return line[0]
break

if you remove the else and just keep the return line[0] has same meaning.

and break at the end is of no use after return!

Upvotes: 1

Related Questions