So for example, in the dictionary, the letter 'A' has a value of .8237
What I wan't to do is replace every letter with the value with .8237 to D in the string (not the dictionary)

I've tried this without success and It's obvious why it wouldn't work:

if sorted(dictionary.values())>=.01:
                code = code.replace(dictionary[letter],'Z')

code = code.replace(letter,'Z')

Ok, this will work for the replacing in the string, last step is to tell it where to replace using the values

Your question is almost impossible to understand. Is it A, D or Z which has 'value' .8237 ? In your code, you are comparing sorted(dictionary.values()), which is a list, to a float, which is meaningless. Can you explain what you want to do with more details ? If we could see example input and expected output, that would help a lot .

It's ok I sort of figured it out, just an inconvenient solution...

for letter in alphabet:
    countcode += code.count(letter)

for letter in alphabet:
        count = code.count(letter)
        freq = (count/countcode)*100

        
        if freq >= 12:
            code = code.replace(letter,'E')

        elif freq >= 9:
            code = code.replace(letter,'T')

        elif freq >= 8:
            code = code.replace(letter,'A')

        elif freq >= 7:
            code = code.replace(letter,'O')

        elif freq >= 6.8:
            code = code.replace(letter,'I')

        elif freq >= 6.4:
            code = code.replace(letter,'N')

        elif freq >= 6.1:
            code = code.replace(letter,'S')

        elif freq >= 6:
            code = code.replace(letter,'H')

        elif freq >= 4.5:
            code = code.replace(letter,'R')

        elif freq >= 4.1:
            code = code.replace(letter,'D')

        elif freq >=4:
            code = code.replace(letter,'L')

        elif freq >=2.782:
            code = code.replace(letter,'C')

        elif freq >= 2.5:
            code = code.replace(letter,'U')

        elif freq >= 2.4:
            code = code.replace(letter,'M')

        elif freq >= 2.3:
            code = code.replace(letter,'W')

        elif freq >= 2.1:
            code = code.replace(letter,'F')

        elif freq >=1.99999999:
            code = code.replace(letter,'G')

        elif freq >= 1.95:
            code = code.replace(letter,'Y')

        elif freq >= 1.6:
            code = code.replace(letter,'P')

        elif freq >= 1:
            code = code.replace(letter,'B')

        elif freq >= .8:
            code = code.replace(letter,'V')

        elif freq >= .3:
            code = code.replace(letter,'K')

        elif freq >= .15:
            code = code.replace(letter,'J')

        elif freq >= .099999999:
            code = code.replace(letter,'X')

        elif freq >= .08:
            code = code.replace(letter,'Q')

        else:
            code = code.replace(letter,'Z')

You can simplify this with module bisect

from bisect import bisect
frequencies = [
    .08, .1, .15, .3, .8, 1, 1.6, 1.95, 2, 2.1,
    2.3, 2.4, 2.5, 2.782, 4, 4.1, 4.5, 6, 6.1, 6.4,
    6.8, 7, 8, 9, 12
]
xletters = "ZQXJKVBPYGFWMUCLDRHSNIOATE"

for letter in alphabet:
        count = code.count(letter)
        freq = (count/countcode)*100
        code = code.replace(letter,xletters[bisect(frequencies, freq)])

I wonder if there is an error in your program's logic, because you are replacing one letter after another in the for loop, and it seems to me that you should replace all the letters at the same time, but since I don't know the rest of your program, I may be wrong.

Using this method, most of the rest of the program is replaced:

from bisect import bisect

code = 'BJ YMJ UJTUQJ TK YMJ ZSNYJI XYFYJX, NS TWIJW YT KTWR F RTWJ UJWKJHY ZSNTS, JXYFGQNXM OZXYNHJ, NSXZWJ ITRJXYNH YWFSVZNQNYD, UWTANIJ KTW YMJ HTRRTS IJKJSXJ, UWTRTYJ YMJ LJSJWFQ BJQKFWJ, FSI XJHZWJ YMJ GQJXXNSLX TK QNGJWYD YT TZWXJQAJX FSI TZW UTXYJWNYD, IT TWIFNS FSI JXYFGQNXM YMNX HTSXYNYZYNTS KTW YMJ ZSNYJI XYFYJX TK FRJWNHF.'
countcode = 0.0
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
frequencies = [
    .08, .1, .15, .3, .8, 1, 1.6, 1.95, 2, 2.1,
    2.3, 2.4, 2.5, 2.782, 4, 4.1, 4.5, 6, 6.1, 6.4,
    6.8, 7, 8, 9, 12
]
xletters = "ZQXJKVBPYGFWMUCLDRHSNIOATE"


for letter in alphabet:
    countcode += code.count(letter)
 
for letter in alphabet:
        count = code.count(letter)
        freq = (count/countcode)*100
        code = code.replace(letter,xletters[bisect(frequencies, freq)])
print code

But, it still seems my frequency is wrong..

After decoding, the code should read: we the people of the united states...

Take the first B in your text. It should be replaced by W, but then when the for loop continues, it will replace this W with something else because W comes after B in the alphabet. Each character should be replaced only once. I think the correct way to do this is to use the method string.translate.

that is way too advanced for me to handle lol, Looks like I'll go back to the drawing board..

that is way too advanced for me to handle lol, Looks like I'll go back to the drawing board..

One thing you could do is replace letters by lowercase letters, because if B is replaced by w, then this w will never be replaced again ;)

OK, well I figured out another way around, but at least I got the output I wanted. Right now I have this output:

"W E T H E P E O P L E O F T H E U N I T E D S T A T E S , I N O R D E R..."

using

print chr(ascii),

how can I get rid of the spaces between the letters without sys?

You can fill a list, and join with the empty string:

mylist = ['W', 'E', 'T', 'H', 'E']
print ''.join(mylist)

ah, perfect thank you so much! Will upload my complete source in a few days!

I always wanted a Vigenere Cipher.

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.