【发布时间】:2018-04-12 04:14:03
【问题描述】:
对于我的编程课,我们必须创建一个函数,该函数接受一个字符串参数并返回该字符串的 rot13 等效项。当我尝试运行我的函数时,它说 count 不能等于 str[i] 因为字符串索引必须是整数。老实说,我迷失了,我还能做些什么来使该功能正常工作。任何帮助都会很可爱
def str_rot_13(str):
new_list = []
for i in str:
if ord(i) <= 77:
count = str[i]
k = chr(ord(count) + 13)
new_list.append(k)
if ord(i) > 77 and ord(i) <= 90:
count = str[i]
k = ord(count) - 78
new_list.append(chr(65 + k))
return new_list
【问题讨论】:
-
count = str[i]的目的是什么? -
@gommb 对我来说,我认为它可以使函数遍历我输入的字符串的每个部分。这不是遍历字符串的正确方法吗?
-
for i in str:将使其遍历 str 中的每个字符。 -
只需删除
count = str[i]并将您使用count的任何位置替换为i -
@gommb 它正在工作,感谢它!
标签: python string indexing indices