By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,712 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,712 IT Pros & Developers. It's quick & easy.

How do I maintain spaces in the Caesar's cipher code

P: 1
I'm trying to create a caesar's cipher that uses raw_input for a sentence and key to encrypt. What I can't figure out is how to maintain spaces as spaces, originally they were treated as though they had a zero value. For example if i typed in a space with a key of 2 it would return b. I can't seem to figure it out.

Expand|Select|Wrap|Line Numbers
  1. import string
  2.  
  3. codedsen = ""
  4.  
  5. idxlower = "abcdefghijklmnopqrstuvwxyz"
  6. sen = raw_input ("Please enter a phrase to encode")
  7. key = raw_input ("Please enter a key number")
  8. print sen
  9. for symbol in sen:
  10.     if symbol is idxlower:
  11.         chidx = idxlower.find(ch) + int(key)
  12.         if chidx > 25:
  13.             chidx = chidx - 26
  14.         elif chidx < 0:
  15.             chidx = chidx + 26
  16.     else:
  17.         chidx = symbol
  18. newch = idxlower[chidx]         <<<< I get an error message 
  19. codedsen = codedsen + newch
  20. print "Your coded phrase is", codedsen
Sep 23 '12 #1
Share this Question
Share on Google+
2 Replies

zmbd
Expert Mod 5K+
P: 5,397
Line 18: What is the specfic error message you are receiving.. number and description.

Line 5: You have excluded the use of the space in this string upon comparison starting at line 9.
Sep 24 '12 #2

Expert 100+
P: 626
You should start with some simple debugging, like these added print statements, so you know how to find simple errors. You want to concentrate on chidx since that is the error message. And note that this modified code does not point out additional errors in the code, and take a look at "Accessing Elements" and "List Membership" here.
Expand|Select|Wrap|Line Numbers
  1. import string
  2.  
  3.  codedsen = ""
  4.  
  5.  idxlower = "abcdefghijklmnopqrstuvwxyz"
  6.  sen = raw_input ("Please enter a phrase to encode")
  7.  key = raw_input ("Please enter a key number")
  8.  print sen
  9.  for symbol in sen:
  10.      if symbol is idxlower:
  11.          print symbol, "is idxlower"
  12.          chidx = idxlower.find(ch) + int(key)
  13.          if chidx > 25:
  14.              chidx = chidx - 26
  15.          elif chidx < 0:
  16.              chidx = chidx + 26
  17.  
  18.      else:
  19.          chidx = symbol
  20.  
  21.      print "chidx =", chidx, "for symbol", symbol
  22.  
  23.  newch = idxlower[chidx]         <<<< I get an error message 
  24.  codedsen = codedsen + newch
  25.  print "Your coded phrase is", codedsen 
Sep 24 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.