此功能的故障在哪里?

发布于 2025-02-11 13:10:04 字数 1078 浏览 1 评论 0原文

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)

该程序有效,但是当我添加功能结构时,为什么它不再起作用?


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)

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2025-02-18 13:10:04

有一些不同的问题。

  1. 范围
  2. 返回值

通过您的自上而下的方法, ,所有变量均已定义。
您的功能方法直接不做任何事情。例如,replace_letters_with_number函数只是将新单词分配到本地变量中,而无需返回。基本上,此功能无济于事。解决此问题的一种方法是返回更新的单词。

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

另一个问题是,当您运行IS_VALID_COLOR变量color技术上不存在。如果我们假设已通过Intno的单词已经替换了此函数,则可以将变量color更改为word

通过这些方法,我们可以通过分配可变颜色来更改for循环的执行方式,当我们调用repents_letters_with_number时。导致:

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

可以做出其他改进;但是,主要问题是变量color尚未定义。

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.

木格 2025-02-18 13:10:04

我发现您的代码错误:

  1. 您的拳头功能是正确的,但没有返回任何内容。这意味着您不能在后续函数中使用此函数产生的单词。 (因此添加)

    返回颜色

  2. 在您的第二个功能中,您必须传递返回的值而不是单词。因为在这种情况下,单词仍然是您阅读的原始单词。

另外,如果您愿意并根据我的观察结果,我建议将功能分为3,以清楚以下。从您的代码中,这是我的理解(如果我弄错了,正确)

  1. 读了一系列单词,并用数字替换特定的字母
  2. ,只有2个和6个
  3. 滤镜单词来自2个字符,而alpha-numeric字符中的字符定义了你。
    以下代码准确地执行此操作。
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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文