【发布时间】:2016-06-05 15:47:17
【问题描述】:
我在 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())
运行代码时出现以下错误:
/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 is not in list
Process finished with exit code 1
第 11 行我在做什么?如何获取字母表中每个字符的以下字符并将其附加到新列表中?
非常感谢。
【问题讨论】:
标签: python string python-2.7