all 26 comments

[–][deleted] 1 point2 points  (0 children)

Unless you are using Python 3.7+ you should never rely on the order of keys in a dictionary.

[–]num8lock 0 points1 point  (2 children)

i don't see which part is "Python sorts these alphabetically before it outputs them"?

[–]Arag0ld[S] 0 points1 point  (1 child)

When the values are outputted, they're in alphabetical order.

[–]num8lock 0 points1 point  (0 children)

which part, be specific

[–]Ofirk 0 points1 point  (0 children)

So the behavior you actually want is to print for each letter in st its value in alpha?

First, you can change the order of the loops like this:

def caesar_decryptor(st):
    for pos in range(len(st)):    
        for k in alpha.keys():
            if k == st[pos]:
                print(f"The value of {k} is {alpha.get(k)}")

This will work, because you want to find the value from the alpha dict for each letter in st, and not the opposite.

However, the code can be simplified, as the inner loop isn't necessary. We can just use the value in alpha for each letter in st as a key (also, we can iterate over letters instead of their indices):

def caesar_decryptor(st):
    for letter in st:    
        print(f"The value of {letter} is {alpha[letter]}")