Change each character in string to the next character in alphabet

前端 未结 8 1123
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 15:45

I am coding in Python 2.7 using PyCharm on Ubuntu.

I am trying to create a function that will take a string and change each character to the character that would be

8条回答
  •  难免孤独
    2021-01-03 16:17

    There's two issues with the code. Instead of looping letters you're looping over numbers since you're calling range(len(str)). The second issue is that within the loop you assign a string to new_word which will cause the next iteration to fail since string doesn't have method append. If you make the following changes it should work:

    for letter in str: # 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)
    new_word = "".join(new_word)
    

提交回复
热议问题