Home > front end >  String index out of range at line 27
String index out of range at line 27

Time:04-06

I'm trying to do a basic Ceaser cipher with non-complicated code and when I type something like Go Buy Bread and set the shift to 8 or higher I get an error called string index out of range and I don't want to use mod (%) can anyone give me a solution?

Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 -!?.'


FinalMessage = ''


print("Cipher/Decipher Code")
print("-")
print("This code can encode messages and decode messages.")


while True:
    print()
    Option = int(input('Type the number 1 to encode your message, number 2 to decode, number 3 to exit: '))  
    print()                                                                                                  
    if Option == 1:  
        Message = list(input("Type the message you want to encode: "))  
        print()                                                         
        print("To encode the message type a number between 1 and 26.")  
        print("The number means how many letters the message will shift.")
        Key = int(input("Number: "))  
        for Cipher in Message:  
            if Cipher in Alphabet:  
                Position = Alphabet.find(Cipher) 
                NewPosition = (Position   Key)
                NewCipher = Alphabet[NewPosition]
                FinalMessage  = NewCipher  
            else:  
                FinalMessage  = Cipher 
        print()
        print("Encrypted Message: "   FinalMessage) 
        print()
        print("------")
        print()
    elif Option == 2:
        print()
        Message = list(input("Type the message you want to decode: "))
        print()
        print("To decode a message type a number between 1 and 26.") 
        print("The number means how many letters the message will shift")  
        Key = int(input("Number: "))  
        Key = (Key * -1) 
        for Cipher in Message:  
            if Cipher in Alphabet: 
                Position = Alphabet.find(Cipher) 
                NewPosition = (Position   Key)
                NewCipher = Alphabet[NewPosition]
                FinalMessage  = NewCipher
            else:  
                FinalMessage  = Cipher 
        print()
        print("Decrypted Message: "   FinalMessage) 
        print()
        print("------")
        print()
    FinalMessage = "" 

CodePudding user response:

Not sure why you wouldn't want to use %, it seems ideal for this. Otherwise, you'll have to manually check if the adjusted index is beyond the range of the alphabet and add/subtract the size of the alphabet to wrap around properly.

The two methods would be:

# Method 1 (preferred).

NewPosition = (Position   Key) % len(Alphabet)

# Method 2 (ugly and unnecessary).

NewPosition = Position   Key
if NewPosition >= len(Alphabet):
    NewPosition -= len(Alphabet)

However, Go Buy Bread with a shift of (positive) eight wouldn't give a wrapping problem with the alpha characters as all the results are still within the string. It may if you subtract eight but I'm assuming here you want to encode the plaintext message rather than decode it.

So your best bet would be to start debugging by printing out Position and NewPosition (and len(Alphabet) as well) each time through the loop. That should hopefully tell you what's happening.

  • Related