【发布时间】:2017-11-05 05:27:34
【问题描述】:
所以我正在尝试为类编写一个函数,该函数接受一个字符串并遍历其中的每个字符,每次遇到“a”“b”或“c”时,它应该将 3 5 和 7 添加到“值”变量(即 a=3 b=5 和 c=7)。然后在字符串的末尾我需要返回 value%11 的剩余部分。
这是我目前所拥有的:
(ps. hash_func 中的所有打印语句都是为了让我可以看到发生了什么)
def hash_func(string):
value=0
string_length= range(len(string))
for i in (string_length):
current_charecter= string[i]
print (current_charecter)
if current_charecter== 'a':
value=value + 3
print (value)
elif current_charecter== 'b':
value=value + 5
print (value)
elif current_charecter== 'c':
value=value + 7
print (value)
elif current_charecter!= 'a' or 'b' or 'c':
value=value+0
else:
value=value%11
print (value)
print(hash_func("banana")) #this is the call to the function that is given by the grader, it should equal 3
函数返回:
b
5
a
8
n
a
11
n
a
14
None
(它只返回“无”而没有额外的打印语句)
所以我知道它正在添加值并正确跳过不是 a b 或 c 的字母,我似乎无法弄清楚“无”的来源。
据我了解,当函数或变量不包含任何内容时,将返回 none 值,所以我不确定为什么在我的函数执行所有内容(大部分)正确后返回它。
如果有人能破译我的密码并告诉我我犯的愚蠢错误,我将不胜感激!
【问题讨论】:
-
请不要破坏您的帖子。发布问题后,您已将内容授权给整个 Stack Overflow 社区(根据 CC-by-SA 许可)。如果您想取消此帖子与您帐户的关联,请参阅What is the proper route for a disassociation request?。
-
抱歉,您不能这样做:D,人们已经花时间回答,为他人创建有用的内容。您需要按照上面链接的正确路线。
-
另外,没有人会复制这项工作。他们可能会复制答案的内容,但不会复制问题的内容。
标签: python-3.x