Reputation: 833
Here is my code:
#!/usr/bin/env python
encodec = {
'a':22 , 'b':24 , 'c':26 , 'd':28 , 'e':12 , 'f':14 , 'g': 16 , 'h':18 , 'i':32 , 'j':34 , 'k':36 , 'l':38 , 'm':42 , 'n':44 , 'o':46 , 'p':48 , 'q':50 , 'r':52 , 's':54 , 't':56 , 'u':58 , 'v': 62 , 'w':64 , 'x':66, 'y': 68 , 'z':72 , ' ':74 , '.':76 , ',':78 , "'":82 , '?':84 , '"':86
}
decodec = {
22:'a' , 24:'b' , 26:'c' , 28:'d' , 12:'e' , 14:'f' , 16:'g' , 18:'h' , 32:'i' , 34:'j' , 36:'k' , 38:'l' , 42:'m' , 44:'n' , 46:'o' , 48:'p' , 50:'q' , 52:'r' , 54:'s' , 56:'t' , 58:'u' , 62:'v' , 64:'w' , 66:'x', 68:'y' , 72:'z' , 74:' ' , 76:"." , 78:',' , 82:"'" , 84:'?' , 86:'"'
}
option = raw_input("Would you like to 'encode' or 'decode' a word? ")
while option.lower() not in ('encode' , 'decode'):
option = raw_input("Please enter a valid response ('encode' or 'decode'): ")
if option.lower() == 'encode':
word = raw_input("Enter a string to be encoded: ").lower()
length = len(word)
z = 1
result = []
while z < length+1:
a = word[z-1]
result += str(encodec[a])
z = z + 1
print int(''.join(result))
elif option.lower() == 'decode':
num = (raw_input("Enter your text to be decoded: "))
length = len(num)
z = 1
result = []
while z < length+1:
a = num[z-1:z+1]
result += str(decodec[int(a)])
z = z + 1
print ''.join(result)
This is the snippet that won't quite work:
elif option.lower() == 'decode':
num = (raw_input("Enter your text to be decoded: "))
length = len(num)
z = 1
result = []
while z < length+1:
a = num[z-1:z+1]
result += str(decodec[int(a)])
z = z + 1
print ''.join(result)
I don't understand why this loop continues to loop. The conditional of the loop is that while the value of variable z is less than the value of the length, to continue the loop. The length doesn't increase in the loop, and I add a line in the loop that adds 1 to z every iteration. For some reason, though, it continues to loop infinitely. I dont understand why?
Upvotes: 0
Views: 155
Reputation: 352979
Looks like inconsistent use of whitespace to me. Try running your code using
python -tt your_program_name.py
to confirm this. If I look at the raw code you've posted, the first snippet looks like this:
" elif option.lower() == 'decode':\n",
' \tnum = (raw_input("Enter your text to be decoded: "))\n',
' \tlength = len(num)\n',
' \tz = 1\n',
' \tresult = []\n',
' \twhile z < length+1:\n',
' \t\ta = num[z-1:z+1]\n',
' \t\tresult += str(decodec[int(a)])\n',
' \tz = z + 1\n',
" print ''.join(result)\n",
' \t\n',
and you can see the strange indentation of the z = z + 1
line. In particular, though it seems like you're using tabs for indentation, that line only has one tab, so even though it looks like it's correctly indented on the screen I don't think it's actually being executed because it's not actually inside the while
loop.
Always use four-space tabs: makes life so much easier. Most editors can be configured to do this.
Upvotes: 6
Reputation: 5122
Does this code work for you?
elif option.lower() == 'decode':
num = (raw_input("Enter your text to be decoded: "))
result = ''
index = 0
while index < len(num) -1:
result += decodec[int(num[index:index +2])]
index += 2
print result
If I set the input as 222426
, the output is abc
Upvotes: 1