Reputation: 1
I am trying to get my code to wrap around the alphabet, for example Z + 1 should = A. Instead I am getting junk values of non-English characters. How do I make my code only use the alphabet and not the junk characters?
enc = "Lbh ner havdhr va gur jbeyq n sevraq yvxr ab bgure Vg vf fb nznmvat gung jr unir fb zhpu va pbzzba fb lbhe unccvarff vf nyfb zvar"
blankList = [] #create the empty list
count = -30
while count < 40: #if less than 40 continue
dec = '' #empty string
for i in enc: #iterarte through
a = ord(i) #convert
b = a + count # create var to store it
dec += chr(b) #to letter
count += 1 #increment count
blankList.append(dec)
for i in blankList: #go through blank list
print i #prints each on own line
Upvotes: 0
Views: 88
Reputation: 2447
You are receiving "junk" characters, because you are asking chr()
to convert a value to a character that is not a normally printed character. This is because you are not wrapping around the edges of the alphabet correctly.
A simple way to see this is to run:
for i in range(255):
print(i, chr(i))
This prints the ASCII code and ASCII character. Those listed with \x
prefix are ASCII value in hexadecimal, because the character isn't normally printed. Many ASCII characters are used for control codes and other things for terminals.
Once you fix the edge wrapping of values past ord('z')
then you should get a handle of this. You also only need to try 25 variations, because there are only 25 characters in the alphabet that can be the other values for this string.
Upvotes: 2