此功能的故障在哪里?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一些不同的问题。
通过您的自上而下的方法, ,所有变量均已定义。
您的功能方法直接不做任何事情。例如,
replace_letters_with_number
函数只是将新单词分配到本地变量中,而无需返回。基本上,此功能无济于事。解决此问题的一种方法是返回更新的单词。另一个问题是,当您运行
IS_VALID_COLOR
变量color
技术上不存在。如果我们假设已通过Intno的单词已经替换了此函数,则可以将变量color
更改为word
。通过这些方法,我们可以通过分配可变颜色来更改for循环的执行方式,当我们调用
repents_letters_with_number
时。导致:可以做出其他改进;但是,主要问题是变量
color
尚未定义。There are a few different issues.
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.Another issue is that when you run the
is_valid_color
the variablecolor
technically does not exist. If we assume that the word being passed intno this function has already been replaced we can change the variablecolor
toword
.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:There are other improvements that could be made; however, the main issue was that the variable
color
was not defined.我发现您的代码错误:
您的拳头功能是正确的,但没有返回任何内容。这意味着您不能在后续函数中使用此函数产生的单词。 (因此添加)
返回颜色
在您的第二个功能中,您必须传递返回的值而不是单词。因为在这种情况下,单词仍然是您阅读的原始单词。
另外,如果您愿意并根据我的观察结果,我建议将功能分为3,以清楚以下。从您的代码中,这是我的理解(如果我弄错了,正确)
以下代码准确地执行此操作。
What I find wrong with your code:
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
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)
The below code does this exactly.