Sam Heather
Sam Heather

Reputation: 1503

Python not returning and setting value

I run the following. all_possible is set to [] at the top of the file.

def a_combination(input):
global current_char
for y in range(0,len(str(typed))):
    working_int = int(list(str(input))[current_char])
    print working_int
    if (len(all_possible) == 0):
        all_possible.append(digits_all[working_int-2][0])
        all_possible.append(digits_all[working_int-2][1])
        all_possible.append(digits_all[working_int-2][2])
    else:
        for x in range(0, len(all_possible)):
            x = 3*x
##            Duplicate to setup 3 of the same words in the list
            all_possible.insert(x, all_possible[x])
            all_possible.insert(x, all_possible[x])
##            Now append the 1st possible character to the first version of the working string etc...
            all_possible[x] = all_possible[x]+digits_all[working_int-2][0]
            all_possible[x+1] = all_possible[x+1]+digits_all[working_int-2][1]
            all_possible[x+2] = all_possible[x+2]+digits_all[working_int-2][2]
##            Optimization - check after each stage for no-matches.
    current_char += 1

print all_possible

    check_matches(all_possible)
print all_possible

def check_matches(input):
    output = []
    for possible_word in input[:]:
        for real_word in word_dictionary:
            if (normalise_word(real_word).startswith(possible_word)):
                output.append(possible_word)
            if (normalise_word(real_word) == possible_word):
                print possible_word, 'is a perfect match with the dictionary.'
    print output
    all_possible = output
    print all_possible
    return all_possible

problem is that it does not return and set the value for all_possible in a_combination. It prints the processed value in check_matches but does not return it correctly, meaning a_combination prints the wrong thing (the list before it went through normalise_word). Any ideas?

Sam

Upvotes: 0

Views: 180

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599698

You don't keep the returned value of all_possible from check_possible(). You assign your output list to a local variable within the function, and return it to the caller, which then throws it away. You should do

all_possible = check_matches(all_possible)

Also, you should stop trying to use global variables, they are the cause of your confusion. Keep your variables local to the functions they are being used in, and choose different names.

Upvotes: 1

Related Questions