Can someone help me solve this string index error in python? -
this code need schools cau, thought had solved keep getting string index error, wondering if tell me whyerror 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 ======================================================== | exit program ---------------------- enter 0 | | find lucky name number ----------- enter 1 | \------------------------------------------------------/ """ option = "" def lucky_name(): forename = input("what forename?") surname = input("what 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
while not option == "0": print(menu) option = input("which option use? ")
if option == "0": print("goodbye!") elif option == "1": lucky_name() elif option == "2": print("whoops. menu option doesn't exist.") print("please try again.")
your code wrong in while totalnum <= len(forename):
the len of array returns lenght of array starting 1 not 0.
if have [1,2,3] len 3 last position 2.
so solve problem change this:
while totalnum < len(forename):
Comments
Post a Comment