I am very new to programming (6 weeks, self taught on the net with "codecademy" and "python the hard way"). I decided it was time I started experimenting at writing code without direction and I hit a wall with my second project.
I am trying to make a "secret coder" that takes a raw_input string and replaces every letters in it with the next one in the alphabet. With my very limited knowledge, I figured that a dictionary should be the way to go. With a "little" googling help, I wrote this:
alpha = {"a" : "b", "b" : "c", "c" : "d", "d" : "e", "e" : "f", "f" : "g","g" : "h"
, "h" : "i", "i" : "j", "j" : "k", "k" : "l", "l" : "m","m" : "n", "n" : "o"
, "o" : "p", "p" : "q", "q" : "r", "r" : "s","s" : "t", "t" : "u", "u" : "v"
, "v" : "w", "w" : "x", "x" : "y", "y" : "z", "z" : "a"}
entry = raw_input("Please write a sentence you want to encode: ")
def encode(entry, letters):
for k, v in letters.iteritems():
if k in alpha:
entry = entry.replace(k, v)
return entry
print encode(entry, alpha)
The problem that I have is that only half the letters in my string are replaced by the correct values from the dictionary. "a" and "b" will both be printed as "c" when "a" should be printed as "b" and "b" should be printed as "c" and so on.
Where I get completely lost is that, when I replaced every value in my dictionary with numbers, it worked perfectly.
That's the gist of it, I really don't get what is wrong with my code.
Thank you in advance for your help.
PS: This was my very first post on stackoverflow, hopefully I did everything the way I should.
EDIT: Since I cannot give reputation yet, I will just thank you all here for your helpful answers. I can see a bit clearer now where my mistake is and I will take the info provided here to fix my code and work on understanding it properly. Also I can see that there are much more logical and straightforward approches to solving this kind of problem. Functions are still a bit blurry to me but I guess it is normal so early on.