有人可以帮我解决python中的这个字符串索引错误吗?

时间:2016-07-12 13:39:16

标签: python python-3.x

这是学校CAU需要的一些代码,我以为我已经解决了它,但我一直收到字符串索引错误,只是想知道是否有人可以告诉我为什么Error message

Letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n","o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

menu = """ 
Caesar Cipher
========================================================
|  To exit the program ---------------------- Enter 0  | 
|  To find your lucky name number ----------- Enter 1  |
\------------------------------------------------------/
"""
option = ""

def lucky_name():




forename = input("What is you forename?")
surname = input("What is your surname?")
forenaem = str(forename)
surnamenum = 0
forenamenum = 0
luckynum = 0
forename = str (forename)
surname = str (surname)
totalnum = 0

while totalnum <= len(forename):

   if forename[totalnum] == Letters[0]:
        forenamenum = forenamenum + 1
   elif forename[totalnum] == Letters[1]:
        forenamenum = forenamenum + 2
   elif forename[totalnum] == Letters[2]:
        forenamenum = forenamenum + 3
   elif forename[totalnum] == Letters[3]:
        forenamenum = forenamenum + 4   
   elif forename[totalnum] == Letters[4]:
        forenamenum = forenamenum + 5
   elif forename[totalnum] == Letters[5]:
        forenamenum = forenamenum + 6
   elif forename[totalnum] == Letters[6]:
        forenamenum = forenamenum + 7
   elif forename[totalnum] == Letters[7]:
        forenamenum = forenamenum + 8
   elif forename[totalnum] == Letters[8]:
        forenamenum = forenamenum + 9
   elif forename[totalnum] == Letters[9]:
        forenamenum = forenamenum + 1
   elif forename[totalnum] == Letters[10]:
        forenamenum = forenamenum + 2
   elif forename[totalnum] == Letters[11]:
        forenamenum = forenamenum + 3
   elif forename[totalnum] == Letters[12]:
        forenamenum = forenamenum + 4
   elif forename[totalnum] == Letters[13]:
        forenamenum = forenamenum + 5
   elif forename[totalnum] == Letters[14]:
        forenamenum = forenamenum + 6
   elif forename[totalnum] == Letters[15]:
        forenamenum = forenamenum + 7
   elif forename[totalnum] == Letters[16]:
        forenamenum = forenamenum + 8
   elif forename[totalnum] == Letters[17]:
        forenamenum = forenamenum + 9
   elif forename[totalnum] == Letters[18]:
        forenamenum = forenamenum + 1
   elif forename[totalnum] == Letters[19]:
        forenamenum = forenamenum + 2
   elif forename[totalnum] == Letters[20]:
        forenamenum = forenamenum + 3
   elif forename[totalnum] == Letters[21]:
        forenamenum = forenamenum + 4
   elif forename[totalnum] == Letters[22]:
        forenamenum = forenamenum + 5
   elif forename[totalnum] == Letters[23]:
        forenamenum = forenamenum + 6
   elif forename[totalnum] == Letters[24]:
        forenamenum = forenamenum + 7
   elif forename[totalnum] == Letters[25]:
        forenamenum = forenamenum + 8
   totalnum = totalnum + 1


totalnum2 = 0    
while totalnum2 <= len(surname):
    if surname[totalnum2] == Letters[0]:
        surnamenum = surnamenum + 1    
    elif surname[totalnum2] == Letters[1]:
        surnamenum = surnamenum + 2
    elif surname[totalnum2] == Letters[2]:
        surnamenum = surnamenum + 3
    elif surname[totalnum2] == Letters[3]:
        surnamenum = surnamenum + 4
    elif surname[totalnum2] == Letters[4]:
        surnamenum = surnamenum + 5
    elif surname[totalnum2] == Letters[5]:
        surnamenum = surnamenum + 6
    elif surname[totalnum2] == Letters[6]:
        surnamenum = surnamenum + 7
    elif surname[totalnum2] == Letters[7]:
        surnamenum = surnamenum + 8
    elif surname[totalnum2] == Letters[8]:
        surnamenum = surnamenum + 9
    elif surname[totalnum2] == Letters[9]:
        surnamenum = surnamenum + 1
    elif surname[totalnum2] == Letters[10]:
        surnamenum = surnamenum + 2
    elif surname[totalnum2] == Letters[11]:
        surnamenum = surnamenum + 3
    elif surname[totalnum2] == Letters[12]:
        surnamenum = surnamenum + 4
    elif surname[totalnum2] == Letters[13]:
        surnamenum = surnamenum + 5
    elif surname[totalnum2] == Letters[14]:
        surnamenum = surnamenum + 6
    elif surname[totalnum2] == Letters[15]:
        surnamenum = surnamenum + 7
    elif surname[totalnum2] == Letters[16]:
        surnamenum = surnamenum + 8
    elif surname[totalnum2] == Letters[17]:
        surnamenum = surnamenum + 9
    elif surname[totalnum2] == Letters[18]:
        surnamenum = surnamenum + 1
    elif surname[totalnum2] == Letters[19]:
        surnamenum = surnamenum + 2
    elif surname[totalnum2] == Letters[20]:
        surnamenum = surnamenum + 3
    elif surname[totalnum2] == Letters[21]:
        surnamenum = surnamenum + 4
    elif surname[totalnum2] == Letters[22]:
        surnamenum = surnamenum + 5
    elif surname[totalnum2] == Letters[23]:
        surnamenum = surnamenum + 6
    elif surname[totalnum2] == Letters[24]:
        surnamenum = surnamenum + 7
    elif surname[totalnum2] == Letters[25]:
        surnamenum = surnamenum + 8
    totalnum2 = totalnum2 + 1

forenamenum = str(forenamenum)
num1 = forenamenum[0]
num2 = forenamenum[1]
num1 = int(num1)
num2 = int(num2)
forenamenum = num1 + num2

surnamenum = str(surnamenum)
num3 = surnamenum[0]
num4 = surnamenum[1]
num3 = int(num3)
num4 = int(num4)
surnamenum = num3 + num4

luckynum = surnamenum + forenamenum

if luckynum >= 10:
    luckynum = str(luckynum)
    num5 = luckynum[0]
    num6 = luckynum[1]
    num5 = int(num5)
    num6 = int(num6)
    luckynum = num5 + num6 

print ("Your name is:") 
print ("Your lucky number is:")
print (luckynum)   
return

虽然没有选项==&#34; 0&#34;:     打印(菜单)     option = input(&#34;您想使用哪个选项?&#34;)

if option == "0":
    print("Goodbye!")

elif option == "1":
    lucky_name()

elif option == "2":
    print("Whoops. That menu option doesn't exist.")
    print("Please try again.")

1 个答案:

答案 0 :(得分:1)

while totalnum <= len(forename):

中的代码错误

数组的len返回数组的长度,从1开始而不是从0开始。

如果你有[1,2,3]则len为3,但最后一个位置为2.

所以要解决你的问题:

while totalnum < len(forename):