0

I joined a course to learn programming with Python. For a certain assignment we had to write the code which I have pasted below.

This part of the code consist of two functions, the first one being make_str_from_row and the second one being contains_word_in_row. As you might have noticed the second function reuses the first function. I already have passed the first function but I cannot pass the second one because when it has to reuse it gives an error about the first function, which is confusing because I did not get any errors for my first function. It says that global variable row_index is not defined.

By the way the second function has been given in a starter code so it cannot be wrong. I don't know what's wrong, especially because I have passed the code which presumable has to be wrong.

I tried asking the team for some feedback in case it might be some error in the grader but it has been a week and I have had no reply while the deadline is 2 days away. I am not asking for answers here I only would like to ask somebody for an explanation about the given error so I can figure out a solution myself. I would really appreciate the help.

def makestrfromrow(board, rowindex):
    """ (list of list of str, int) -> str

    Return the characters from the row of the board with index row_index
    as a single string.

    >>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
    'ANTT'
    """

    string = ''
    for i in board[row_index]:
        string = string + i
    return string

def boardcontainswordinrow(board, word):
    """ (list of list of str, str) -> bool

    Return True if and only if one or more of the rows of the board contains
    word.

    Precondition: board has at least one row and one column, and word is a
    valid word.

    >>> board_contains_word_in_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'SOB')
    True
    """

    for row_index in range(len(board)):
        if word in make_str_from_row(board, row_index):
            return True

    return False
3
  • When posting Python questions, you absolutely must doublecheck your indentation. Your code samples are obviously broken and in Python it's generally impossible for anyone else to say how they're supposed to work. Commented Oct 5, 2013 at 15:46
  • 2
    Have a close look how your variables are named. Hint: The _ (underscore) is relevant. Commented Oct 5, 2013 at 15:46
  • Your makestrfromrow sure is a long-winded way to say "".join(board[rowindex])... Commented Oct 5, 2013 at 16:03

1 Answer 1

4

You named the argument rowindex but use the name row_index in the function body.

Fix one or the other.

Demo, fixing the name used in the body of the function to match the argument:

>>> def makestrfromrow(board, rowindex):
...     string = ''
...     for i in board[rowindex]:
...         string = string + i
...     return string
... 
>>> makestrfromrow([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
'ANTT'

Do note that both this function and boardcontainswordinrow are not consistent with the docstring; there they are named as make_str_from_row and board_contains_word_in_row. Your boardcontainswordinrow function uses make_str_from_row, not makestrfromrow, so you'll have to correct that as well; one direction or the other.

Sign up to request clarification or add additional context in comments.

8 Comments

For some reason that must have happened after posting. I checked my code and both the argument name and the variable name have the underscore.
The error you posted is entirely consistent with this mistake.
I dont understand. The original code does have the underscores so it has to be a different reason.
@user2756101: Then use the version without underscores in boardcontainswordinrow too. You have to stick to one or the other.
No that is not what I mean. All the codes have the underscores I have double-checked this. When I posted the code on this website some of the underscores disappeared. So the underscores are at the right place. I also just got a reaction from the team but they say that they are just as baffled as I am because their code exactly matches mine and has passed all the tests.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.