Hey, Guys and Gals.
For some reason the variable text_list seems to be modified after the variable final has been modified.
text_list is a list of text :)
Full code is below. Any other hints and tips would be great
Thanks a bunch.

decode_list = []
key = 0
final = text_list
loop = 0

while key <= len(chars):

    for x in text_list:

        if x in chars:
            final[loop] = chars[int((chars.index(x) - key) % len( chars ))]

        loop += 1

     decode_list.append(''.join( final ))
     key += 1         
     loop = 0

---------------------------------------------------------------------

# Caesar Cipher Decrypt/Encrypt Program
# Used to decrypt/encrypt Caesar Ciphers.
# -- IMPORTS --
import string
# -- VARIABLES --
# -- OPERATIONS --
ENCODE = 76765
DECODE = 89671
# -- CHARACTER ENCODINGS --
BASIC_ALPHABET = list(string.ascii_letters)
VOWELS = [ 'a', 'e', 'i', 'o', 'u', 'A', 'I', 'E', 'O', 'U' ]

# Code Start:
text_in = raw_input('Please enter the text you wish to be decoded/encoded: \n')  # Enter the text you wish to change.

def RepresentsInt(s): # Write own for v0.3
    try: 
        int(s)
        return True
    except ValueError:
        return False


def ask_operation():

    operation_q = raw_input('What operation would you like to perform: [e]ncode or [d]ecode: ').lower() # Ask what they would like to do.

    if operation_q in ('d', 'decode'):
        return DECODE

    elif operation_q in ('e', 'encode'):
        return ENCODE

def encode( text ):

    chars = ''
    shift = -1
    text_list = list( text )

    while chars == '':
        char_set = raw_input('default - 26 letter alphabet(upper and lower) |\nWhat character encodeing would you like to use: ' )
        if char_set == '':
            chars = BASIC_ALPHABET

    while shift not in range(1, len(chars)):
        shift = int(raw_input('What character shift would you like ( 1 - ' + str( len( chars ) + 1 ) + ' ): '))

    final = text_list

    loop = 0

    for x in text_list:
        if x in chars:
            final[loop] = chars[int((chars.index(x) + shift) % len( chars ))]
        loop +=1
    encoded = ''.join( final )
    print encoded

def decode( text ):

    chars = ''
    key = 1097962
    text_list = list( text )

    while chars == '':

        char_set = raw_input('default - 26 letter alphabet(upper and lower) |\nWhat character encodeing would you like to use: ' )

        if char_set == '':
            chars = BASIC_ALPHABET

    while True:

        key = raw_input('What is the Decryption key ( 1 - ' + str( len( chars ) + 1 ) + ' or leave blank if unknown ): ')

        if RepresentsInt( key ):

            key = int( key )
            final = text_list
            loop = 0

            for x in text_list:

                if x in chars:

                    final[loop] = chars[int((chars.index(x) - key) % len( chars ))]
                loop += 1
                decoded = ''.join( final )    

            print decoded

            break

        elif key == "":

            # Brute Force
            decode_list = []
            key = 0
            final = text_list
            loop = 0

            while key <= len(chars):
                for x in text_list:
                    if x in chars:
                        final[loop] = chars[int((chars.index(x) - key) % len( chars ))]
                    loop += 1
                decode_list.append(''.join( final ))
                key += 1

                loop = 0


            vowel_list = []
            print decode_list
            for phrases in decode_list:
                phrase_vowel = 0
                word_vowel = 0
                word_list = phrases.split()

                for words in word_list[:]:
                    vowel = 0
                    letter_list = list(words)

                    for letter in letter_list[:]:

                        if letter in VOWELS:
                            vowel += 1

                    if vowel > 0:
                        word_vowel += 1

                if word_vowel > int(round(len(word_list)*0.9)):
                    vowel_list.append( phrases )

            if vowel_list:
                vowel_list.sort()
                last = vowel_list[-1]
                for i in range(len(vowel_list)-2, -1, -1):
                    if last == vowel_list[i]:
                        del vowel_list[i]
                    else:
                        last = vowel_list[i]

            print vowel_list
            break


operation = ''

while operation == '':
    operation = ask_operation()

if operation == ENCODE:
    encode( text_in )

elif operation == DECODE:
    decode( text_in )

Recommended Answers

All 4 Replies

You define text_list to point to the same block of memory as final.

final = text_list

If you want want a copy, use copy or deepcopy

In line 107 you append final to decode_list multiple times. Break the program down into small functions that you can test individually, as fixing one error will probably just lead to many more that you can not find because you can not test each group of code the way it is now. Like Tony, 157 lines of code is more than I care to wade through so post back if there are any other problems that you can not fix.

 decode_list.append(''.join( final ))

Yay! Thank you very much. I didn't expect anyone to read the entire code, but thanks for the function tip. Anyway here is a working-ish code that will brute force solve a caeser cipher. Thanks again.

# -- IMPORTS --
import string
import copy
# -- VARIABLES --
# -- OPERATIONS --
ENCODE = 76765
DECODE = 89671
# -- CHARACTER ENCODINGS --
BASIC_ALPHABET = list(string.ascii_letters)
VOWELS = [ 'a', 'e', 'i', 'o', 'u', 'y', 'Y', 'A', 'I', 'E', 'O', 'U' ]

# Code Start:
text_in = raw_input('Please enter the text you wish to be decoded/encoded: \n')  # Enter the text you wish to change.

def RepresentsInt(s): # Write own for v0.3
    try: 
        int(s)
        return True
    except ValueError:
        return False


def ask_operation():

    operation_q = raw_input('What operation would you like to perform: [e]ncode or [d]ecode: ').lower() # Ask what they would like to do.

    if operation_q in ('d', 'decode'):
        return DECODE

    elif operation_q in ('e', 'encode'):
        return ENCODE

def encode( text ):

    chars = ''
    shift = -1
    text_list = list( text )

    while chars == '':
        char_set = raw_input('default - 26 letter alphabet(upper and lower) |\nWhat character encodeing would you like to use: ' )
        if char_set == '':
            chars = BASIC_ALPHABET

    while shift not in range(1, len(chars)):
        shift = int(raw_input('What character shift would you like ( 1 - ' + str( len( chars ) + 1 ) + ' ): '))

    final = text_list

    loop = 0

    for x in text_list:
        if x in chars:
            final[loop] = chars[int((chars.index(x) + shift) % len( chars ))]
        loop +=1
    encoded = ''.join( final )
    print encoded

def decode( text ):

    chars = ''
    key = 1097962
    text_list = list( text )

    while chars == '':

        char_set = raw_input('default - 26 letter alphabet(upper and lower) |\nWhat character encodeing would you like to use: ' )

        if char_set == '':
            chars = BASIC_ALPHABET

    while True:

        key = raw_input('What is the Decryption key ( 1 - ' + str( len( chars ) + 1 ) + ' or leave blank if unknown ): ')

        if RepresentsInt( key ):

            key = int( key )
            final = copy(text_list)
            loop = 0

            for x in text_list:

                if x in chars:

                    final[loop] = chars[int((chars.index(x) - key) % len( chars ))]
                loop += 1
                decoded = ''.join( final )    

            print decoded

            break

        elif key == "":

            # Brute Force
            # BUG text_list keeps changing.
            decode_list = []
            key = 0
            final = copy.copy(text_list)
            loop = 0

            while key <= len(chars):
                for x in text_list:
                    if x in chars:
                        final[loop] = chars[int((chars.index(x) - key) % len( chars ))]
                    loop += 1
                decode_list.append(''.join( final ))
                key += 1

                loop = 0

            vowel_list = []

            # For loop seems to change the value
            for phrases in decode_list:
                phrase_vowel = 0
                word_vowel = 0
                word_list = phrases.split()

                for words in word_list:
                    vowel = 0
                    letter_list = list(words)

                    for letter in letter_list:

                        if letter in VOWELS:
                            vowel += 1

                    if vowel > 0:
                        word_vowel += 1
                if word_vowel >= int(round(len(word_list)*0.9)):
                    vowel_list.append( phrases )

            if vowel_list: # Make a function for removeing duplicates from a list. without deleting them from the list. returning a new clean list.
                vowel_list.sort()
                last = vowel_list[-1]
                for i in range(len(vowel_list)-2, -1, -1):
                    if last == vowel_list[i]:
                        del vowel_list[i]
                    else:
                        last = vowel_list[i]

            print vowel_list
            break






operation = ''

while operation == '':
    operation = ask_operation()

if operation == ENCODE:
    encode( text_in )

elif operation == DECODE:
    decode( text_in )

I would strongly suggest that you take another look at the encode function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.