小编Vit*_*ito的帖子

将字符串中的每个字符更改为字母表中的下一个字符

我在 Ubuntu 上使用 PyCharm 在 Python 2.7 中编码。

我正在尝试创建一个函数,该函数将接受一个字符串并将每个字符更改为字母表中的下一个字符。

def LetterChanges(str):
    # code goes here
    import string
    ab_st = list(string.lowercase)
    str = list(str)
    new_word = []
    for letter in range(len(str)):
        if letter == "z":
            new_word.append("a")
        else:
            new_word.append(ab_st[str.index(letter) + 1])
        new_word = "".join(new_word)
    return new_word


# keep this function call here
print LetterChanges(raw_input())
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,出现以下错误:

/usr/bin/python2.7 /home/vito/PycharmProjects/untitled1/test.py
test
Traceback (most recent call last):
  File "/home/vito/PycharmProjects/untitled1/test.py", line 17, in <module>
    print LetterChanges(raw_input())
  File "/home/vito/PycharmProjects/untitled1/test.py", line 11, in LetterChanges
    new_word.append(ab_st[str.index(letter) + 1])
ValueError: 0 …
Run Code Online (Sandbox Code Playgroud)

python string python-2.7

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×1

python-2.7 ×1

string ×1