473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
2 5854
zmbd
5,501 Expert Mod 4TB
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
dwblas
626 Expert 512MB
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

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

Similar topics

135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
3
by: ApexData | last post by:
The editor eliminates spaces in my code. Is there a setting or technique for stopping this behavior. I want to be able to space out my code for cleaner reading? ' I Like This:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.