Home > Enterprise >  Where is the fault in this function?
Where is the fault in this function?

Time:06-29

with open("german.txt") as f:
    words = f.read().split()

for word in words:
    color = word.lower().replace("o", "0").replace("i", "1").replace("s", "5").replace("t", "7")
    if len(word) == 3 or len(word) == 6:
        ok = True
        for c in color:
            if c not in "abcdef0123456789":
                ok = False
                break
        if ok:
            print(word, "#"   color)

This program works, but why doesn't it work anymore when I add a function structure to it?


with open("german.txt") as f:
    words = f.read().split()
          
            
def replace_letters_with_numbers(word):
    color = word.lower().replace("o", "0").replace("i", "1").replace("s", "5").replace("t", "7")

    
def is_valid_color(word):
    if len(word) == 3 or len(word) == 6:
        ok = True
        for c in color:
            if c not in "abcdef0123456789":
                ok = False
                break
        if ok:
            print(word, "#"   color)
         
for word in words:
    replace_letters_with_numbers(word)
    is_valid_color(word)

Thanks in advance!

CodePudding user response:

There are a few different issues.

  1. Scoping
  2. Return values

With your top-down approach, all the variables are defined. Your functional approach straight up just not doing anything. For example, the replace_letters_with_number function is just assigning the new word into a local variable and returning nothing. Basically this function does nothing. One way to solve this is to return the updated word.

def replace_letters_with_numbers(word):
    return word.lower().replace("o", "0").replace("i", "1").replace("s", "5").replace("t", "7")

Another issue is that when you run the is_valid_color the variable color technically does not exist. If we assume that the word being passed intno this function has already been replaced we can change the variable color to word.

With these approaches we could change how the for loop is executed, by assigning a variable color when we call replace_letters_with_number. Resulting in:

for word in words:
    color = replace_letters_with_numbers(word)
    is_valid_color(color)

There are other improvements that could be made; however, the main issue was that the variable color was not defined.

CodePudding user response:

What I find wrong with your code:

  1. your fist function is correct but is does not return anything. this means you cannot use the word resulting from this function in the subsequent functions. (so add )

    return color

  2. in your second function you must pass the returned value instead of word. Because word will still be in this case the raw word you read.

Also if you prefer and based on my observation I suggest splitting the functions into 3 for clarity as below. And from you code this is my understanding (correct if i got this wrong)

  1. read a series of words and replace specific letters with numbers
  2. filter only words of length 3 and 6
  3. filter words from 2 with characters that are within the alpha-numeric characters defined by you. The below code does this exactly.
def color_word(word):
    color = word.lower().replace("o", "0").replace("i", "1").replace("s", "5").replace("t", "7")
    return color
    
def word_length_is_3_and_6(word):
    ok = False
    if len(word) == 3 or len(word) == 6:
        ok = True
        print(f'{word} has {len(word)} letters \n resulting color from word : {color_word(word)}')
    return ok

def check_alpha_numeric_state(word):
    ok=True
    for each_char in word:
        if each_char not in "abcdef0123456789":
            ok = False
            print('all letters in word are not alphanumeric - [abcdef0123456789]')
            break
        else:
            print(word, "#"   color_word(word))

if __name__ == '__main__':
    words = ['happil', 'nos', 'Trusts' 'shon']

    for each_word in words:
        colorword = color_word(each_word) #returns the word with replaced letters and numbers
        word_length = word_length_is_3_and_6(each_word) #boolean
        if word_length == True:
            check_alpha_numeric_state(colorword)
  • Related