Python 3.x 无法理解函数返回“的原因”;无”;
因此,我尝试为类编写一个函数,该函数接受一个字符串并遍历其中的每个字符,每次遇到“a”“b”或“c”时,它都应该向“value”变量添加3 5和7(即a=3 b=5和c=7)。然后在字符串的末尾,我需要返回值%11的剩余部分 这就是我到目前为止所做的: (注:hash_func中的所有print语句都只是为了让我看到发生了什么) 函数返回:Python 3.x 无法理解函数返回“的原因”;无”;,python-3.x,Python 3.x,因此,我尝试为类编写一个函数,该函数接受一个字符串并遍历其中的每个字符,每次遇到“a”“b”或“c”时,它都应该向“value”变量添加3 5和7(即a=3 b=5和c=7)。然后在字符串的末尾,我需要返回值%11的剩余部分 这就是我到目前为止所做的: (注:hash_func中的所有print语句都只是为了让我看到发生了什么) 函数返回: b 5 a 8 n a 11 n a 14 None (它只返回“None”,不包含额外的打印语句) 因此,我知道这是在添加值,并跳过不正确的字母a、b或
b
5
a
8
n
a
11
n
a
14
None
(它只返回“None”,不包含额外的打印语句)
因此,我知道这是在添加值,并跳过不正确的字母a、b或c,我似乎无法理解“无”是从哪里来的
据我所知,none值是在函数或变量不包含任何内容时返回的,因此我不确定为什么在我的函数执行所有(大部分)正确的操作后返回
如果有人能破译我的密码,告诉我我犯了什么愚蠢的错误,我将不胜感激 它返回
None
,因为它不返回任何其他内容。停止打印返回值。它返回None
,因为它不返回任何其他内容。停止打印返回值。对于您的问题,您的函数不返回任何内容,因此默认情况下会返回None
还要注意你的代码是错误的,这里是解释
def hash_func(string):
value = 0
# It will iterate over the chars in the string and set ch to each char in
# turn
for ch in string:
if ch == 'a':
value += 3 # same thing as value = value + 3
elif ch == 'b':
value += 5
elif ch == 'c':
value += 7
print(ch, value)
# You don't need this and it's wrong also (note 1)
# elif ch != 'a' or 'b' or 'c':
# value += 0
# After you've iterated over the string, only then do you modulo it.
# Otherwise you're performing a modulo every time you run into a character
# that's not 'a', 'b' or 'c'
return "Hash is " + str(value % 11)
print(hash_func("banana"))
注意1:当您有一个if语句时,您可以使用else
来匹配与先前条件不匹配的任何内容。如果您检查ch==“a”
则无需检查ch!='a'
还有,做什么
ch == 'a' or X or Y
不会做你认为它会做的事
它实际上在做的是
is ch equal to 'a'? Yes, ok condition is true
otherwise, is X true? or condition is true
otherwise, is Y true? or condition is true
otherwise condition is false
看
如果您想检查ch是否是'a','b','c'中的一个
您可以使用类似于ch-in('a','b','c')
另外,供您参考
def better_hash_func(string):
values = {'a': 3,
'b': 5,
'c': 7}
value = 0
for ch in string:
if (ch in values.keys()):
value += values[ch]
return "Hash is " + str(value % 11)
def even_better_hash_func(string):
values = {'a': 3,
'b': 5,
'c': 7}
# We use the sum function to sum the list
value = sum(
[values[ch] # This is list comprehension
for ch in string # It create a list, by iterating over
if ch in values.keys() # some iterable (like a list)
] # This is very similar to a for loop,
) # but it takes a value you produce each
# iteration and adds that
# to a new list that is returned by the
# list comprehension.
return "Hash is " + str(value % 11)
有关列表理解的更多信息:问题的答案是,函数不返回任何内容,因此默认情况下会返回None
还要注意你的代码是错误的,这里是解释
def hash_func(string):
value = 0
# It will iterate over the chars in the string and set ch to each char in
# turn
for ch in string:
if ch == 'a':
value += 3 # same thing as value = value + 3
elif ch == 'b':
value += 5
elif ch == 'c':
value += 7
print(ch, value)
# You don't need this and it's wrong also (note 1)
# elif ch != 'a' or 'b' or 'c':
# value += 0
# After you've iterated over the string, only then do you modulo it.
# Otherwise you're performing a modulo every time you run into a character
# that's not 'a', 'b' or 'c'
return "Hash is " + str(value % 11)
print(hash_func("banana"))
注意1:当您有一个if语句时,您可以使用else
来匹配与先前条件不匹配的任何内容。如果您检查ch==“a”
则无需检查ch!='a'
还有,做什么
ch == 'a' or X or Y
不会做你认为它会做的事
它实际上在做的是
is ch equal to 'a'? Yes, ok condition is true
otherwise, is X true? or condition is true
otherwise, is Y true? or condition is true
otherwise condition is false
看
如果您想检查ch是否是'a','b','c'中的一个
您可以使用类似于ch-in('a','b','c')
另外,供您参考
def better_hash_func(string):
values = {'a': 3,
'b': 5,
'c': 7}
value = 0
for ch in string:
if (ch in values.keys()):
value += values[ch]
return "Hash is " + str(value % 11)
def even_better_hash_func(string):
values = {'a': 3,
'b': 5,
'c': 7}
# We use the sum function to sum the list
value = sum(
[values[ch] # This is list comprehension
for ch in string # It create a list, by iterating over
if ch in values.keys() # some iterable (like a list)
] # This is very similar to a for loop,
) # but it takes a value you produce each
# iteration and adds that
# to a new list that is returned by the
# list comprehension.
return "Hash is " + str(value % 11)
有关列表理解的更多信息:请不要破坏您的帖子。一旦您发布了一个问题,您就已经将内容授权给了Stack Overflow社区(在CC by SA许可下)。如果您想取消此帖子与您的帐户的关联,请参阅。很抱歉,您不能这样做:D,人们已经花时间回答,为他人创建有用的内容。您需要遵循上面链接的正确路线。此外,没有人会复制此作品。他们可能会抄袭答案,但不会抄袭问题。请不要破坏你的帖子。一旦您发布了一个问题,您就已经将内容授权给了Stack Overflow社区(在CC by SA许可下)。如果您想取消此帖子与您的帐户的关联,请参阅。很抱歉,您不能这样做:D,人们已经花时间回答,为他人创建有用的内容。您需要遵循上面链接的正确路线。此外,没有人会复制此作品。他们可能会抄袭答案,但不会抄袭问题的答案。