Hello,

I'm creating a word guess game for an assignment. I am nearly finished, but I am having difficulties with one function in particular.

I have a function that checks if a str is part of a list of list of str and returns true of false.

Now I am trying to create another function that counts the number of str in a nested list, but I am having difficulty doing so:

The code:

'''A board is a list of list of str. For example, the board
    ANTT
    XSOB
is represented as the list
    [['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']]

A word list is a list of str. For example, the list of words
    ANT
    BOX
    SOB
    TO
is represented as the list
    ['ANT', 'BOX', 'SOB', 'TO']
'''


def is_valid_word(wordlist, word):
    ''' (list of str, str) -> bool

    Return True if and only if word is an element of wordlist.

    >>> is_valid_word(['ANT', 'BOX', 'SOB', 'TO'], 'TO')
    True
    '''
    i = ''
    for i in wordlist:
      return word in wordlist


def make_str_from_row(board, row_index):
    ''' (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'
    '''
    a = ''
    i = ''
    for i in board:
        a = board[row_index]
        return ''.join(a)


def make_str_from_column(board, column_index):
    ''' (list of list of str, int) -> str

    Return the characters from the column of the board with index column_index
    as a single string.

    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
    'NS'
    '''

    #a = ''
    #for i in board:
    #    a = board[i][column_index] + board[i + 1][column_index]
    #    return ''.join(a)

    L = board
    total = ''
    for index in range(len(L)):
        b = board[index][column_index]
        total = total + b
    return total



def board_contains_word_in_row(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


def board_contains_word_in_column(board, word):
    ''' (list of list of str, str) -> bool

    Return True if and only if one or more of the columns 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_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'NO')
    False
    '''
    for column_index in range(len(board)):
        if word in make_str_from_column(board, column_index):
            return True

    return False

def board_contains_word(board, word):
    '''(list of list of str, str) -> bool

    Return True if and only if word appears in board.

    Precondition: board has at least one row and one column.

    >>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
    True
    '''
    a = ''
    for word in range(len(board)):
        if a in make_str_from_row(board, word):
            return True


    return False

def word_score(word):
    '''(str) -> int

    Return the point value the word earns.

    Word length: < 3: 0 points
                 3-6: 1 point per character in word
                 7-9: 2 points per character in word
                 10+: 3 points per character in word

    >>> word_score('DRUDGERY')
    16
    '''
    points = 0
    i = 0
    if len(word) <= 2:
        return 0
    elif len(word) <= 6:
        points = len(word) * 1
        return points
    elif len(word) <= 9:
        points = len(word) * 2
        return points
    elif len(word) >= 10:
        points = len(word) * 3
        return points


def update_score(player_info, word):
    '''([str, int] list, str) -> NoneType

    player_info is a list with the player's name and score. Update player_info
    by adding the point value word earns to the player's score.

    >>> update_score(['Jonathan', 4], 'ANT')
    '''

    player_info[1] += word_score(word)



def num_words_on_board(board, words):
    '''(list of list of str, list of str) -> int

    Return how many words appear on board.

    >>> num_words_on_board([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], ['ANT', 'BOX', 'SOB', 'TO'])
    3
    '''
    num = 0

    for i in range(len(board)):
        if board_contains_word(board, words) == True:
            num = num + 1
            return num




def read_words(words_file):
    ''' (file open for reading) -> list of str

    Return a list of all words (with newlines removed) from open file
    words_file.

    Precondition: Each line of the file contains a word in uppercase characters
    from the standard English alphabet.
    '''

    #words_file = open('C:/Users/diske/Desktop/words_file.txt', 'r')
    #words_file2 = [item.rstrip() for item in words_file]
    #print(words_file2)

    #words_file = askopenfile(mode='r', title='Select word list file')
    #words = a3.read_words(words_file)

    words_file2 = [item.rstrip() for item in words_file]
    return words_file2


def read_board(board_file):
    ''' (file open for reading) -> list of list of str

    Return a board read from open file board_file. The board file will contain
    one row of the board per line. Newlines are not included in the board.
    '''
    #words_file = askopenfile(mode='r', title='Select word list file')
    #words = a3.read_words(words_file)

    #board_file = open('C:/Users/diske/Desktop/board_file.txt', 'r')
    #board_file2 = [item.rstrip('\n') for item in board_file]
    #print(board_file2)

    line = board_file.readline()
    while line != '':
        print(line, end='')
        line = board_file.readline()

As you can see, the problematic portion is clearly num_words_on_board. What I would like to write is:
PER OCCURENCE of board_contains_word(board, words) == True:, increase my counter by one. So far a true is returned if, at least one, str is part of the list, but the num never grows past 1.

I'd be very appreciative for any help/hints/tips/advice.

Thanks.

When will the variable "a" be found in the following code?

    a = ''
    for word in range(len(board)):
        ## do you really want to look for a=''
        if a in make_str_from_row(board, word):
            return True

I have a function that checks if a str is part of a list of list of str and returns (T)rue or (F)alse

The first problem is that "BOX" won't be found in "BOSX" so you have to search letter by letter and keep track of what is found (and add some tests so you know where the problems are). That brings up a second problem, finding a letter only once. So you should not find "keep" in "kept" because the "e" should be eliminated after the first find.

def find_word(input_word):
    list_of_letters=[['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B'], 
    ["K", "E", "P", "T"]]

    for el in list_of_letters:
        print "\ntesting for", input_word, el
        el_copy = el[:]
        found_letters=[]     ## holds all letters found
        for ltr in input_word:
            if ltr in el_copy:
                print "Found", ltr
                found_letters.append(ltr)
                el_copy.remove(ltr) ## remove letter ==> can't be found again
        if len(found_letters) == len(input_word):
            print input_word, "***** found in", el, "*****"

for word in ["BOX", "KEEP"]:
    find_word(word)

Once again, add some tests for each function so you know what is happending.

Hello and thank you for your help.

The example calls were provided to us. In the num_words_on_board() function it was this one:

num_words_on_board([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], ['ANT', 'BOX', 'SOB', 'TO'])
3

As you can see it returns three. ANT and SOB are supposed to be found in the row, and TO is supposed to be found from the column. Apparently, it's no problem if BOX is not found.

After doing some more checking, I think there is an issue with my board_contains_word_in_column function as it provides the correct value for

board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'NO')
--> false

and
board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'AN')
--> true
, but not

board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'TB')
--> false (while it should, obviously, be true).

Since I've been programming for a double digit amount of hours, without stopping for logner than a toilet break, I think I should take a breather for now.

Thanks for your reply.

guys don't help him!
this is an assignment for a coursera class and he should not ask for help

As stated, it is for an assignment and I am not asking for outright answers, merely some pointers into the right direction. If it were for an in person university class it would be possible to set up some local meet ups etc. The forum is pretty dead for this assignment, sadly.

and
board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'AN')
--> true

This should be False for column words.

Print column_index as I don't think it is what you think it is.

def board_contains_word_in_column(board, word):
    for column_index in range(len(board)):
        print "column index", column_index

Again, add some tests so you know where the problem is. The obstacle you face is not the coding. The obstacle you face is that you have code that has not been tested.

sorry for the small mistake in the above code
its

for column_index in range(len(board)):
        if word in make_str_from_column(board, column_index):
            return True
        else:
            return False

there is no column_index for word

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.