'Vinegere cypher, Python. Index issue with special characters and white spaces. Any suggestions?

So I been working on this Vigenere decryption. I managed to get an output, but the output is wrong due to the index being thrown off by special characters and white spaces. I was wondering if anyone had any suggesting in keep the encryption key aligned with the secret message as it iterates through the index.

import string

keys = list(string.ascii_lowercase)
def decoder(coded_message, offset):
    
    real_message = []
    real_index = 0
    real_letter = []
    new_index = 0
    index = 0

    for i in range(0, len(coded_message)):
        if coded_message[i].isalpha() != True:
            real_letter.append(coded_message[i])
        else:
            new_index = offset_key(offset, i)
            index = keys.index(coded_message[i])
            real_index = (index - new_index) % 26
            real_letter.append(keys[real_index])
            real_message = "".join(real_letter)

    return(real_message)

keyword = "friends"
fourth_message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"

def offset_finder(keyword):
    offset_list = []
    for letter in keyword:
        offset_list.append(keys.index(letter))
    return offset_list

new_offset = offset_finder(keyword)
print(new_offset)

def offset_key(new_offset, index):
    index = (index % 7)
    offset = new_offset[index]
    return offset


f_decoded_message = decoder(fourth_message, new_offset)
print(f_decoded_message)



Solution 1:[1]

fourth_message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
clean_message = []
decoded_fourth_message = []
pract_message = []
stripped_message = []
inserted_message = []



def offset_finder(keyword):
    offset_list = []
    for letter in keyword:
        offset_list.append(keys.index(letter))
    return offset_list

new_offset = offset_finder(keyword)
print(new_offset)

def offset_key(new_offset, index):
    index = (index % 7)
    offset = new_offset[index]
    return offset

def cleaner(coded_message):
    punct = []
    no_space = []
    punct_index = []
    new_space =[]
    g = 0
    j = 0
    for i in range(len(coded_message)):
        no_space += coded_message[i]
        
        if coded_message[i].isalpha() != True or coded_message[i] == " ":
            punct_index.append(i)
            punct.append(coded_message[i])
            j -= 1
            del no_space[j]
            
        else:
            continue
        j+= 1
        
    return no_space
        

        
def V_decoder(coded_message, offset):
    real_message = []
    real_index = 0
    real_letter = []
    new_index = 0
    index = 0
  
    
    for i in range(0, len(coded_message)):
        #if coded_message[i].isalpha() != True:
            #real_letter.append(coded_message[i])
            
            new_index = offset_key(offset, i)
            index = keys.index(coded_message[i])
            real_index = (index - new_index) % 26
            real_letter.append(keys[real_index])
            #real_message = "".join(real_letter)

    return(real_message)




clean_message = cleaner(fourth_message)
#print(clean_message)
stripped_message = decoder(clean_message, new_offset)
changing_message = []



def inserter(fourth_message, stripped_message):
    join_message= []
    punct = []
    punct_index = []
    j = 0
    index = 0
    s = 0
    decoded_v =[]
    for i in range(len(fourth_message)):
        index += 1
        if fourth_message[i].isalpha() != True or fourth_message[i] == " ":
            punct_index.append(i)
            punct.append(fourth_message[i])
            
        else:
            continue
            
    print(punct)
    print(punct_index)
    
    for i in range(len(fourth_message)):
        if i == punct_index[j]:
            join_message.insert(i, punct[j])
            j+=1
        else:
            join_message.append(stripped_message[s])
            s += 1
            
        decoded_v ="".join(join_message)
        
    #print(punct_index)
    #print(punct)
    #print(index)
    return(decoded_v)

pract_message = inserter(fourth_message, stripped_message)
print(pract_message)  


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fullmetal04