Python code help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 18
Reputation: DEATHMASTER is an unknown quantity at this point 
Solved Threads: 0
DEATHMASTER DEATHMASTER is offline Offline
Newbie Poster

Python code help

 
0
  #1
Sep 27th, 2009
Hi everyone. I'm new here and would like some help with some Python code. I'm trying to use modulus to shift decode an input line (regular sentence or word like "Bill") but I keep going over the regular alphabet in ascii and the "B" becomes an "=" How do I make "B" become "W" or any other letter that goes over the regular alphabet?
  1. c_string = raw_input("Enter desired sentence to convert: ")
  2.  
  3. cs_string = 5
  4.  
  5. ci_string = cs_string % 26
  6.  
  7. upper_string = c_string.upper()
  8.  
  9. for ch in upper_string:
  10. ascii = ord(ch)
  11.  
  12. if ch.isalpha():
  13. ascii -= cs_string
  14. if ascii > ord("z"):
  15. ascii -= 26
  16.  
  17. print chr(ascii),
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Python code help

 
0
  #2
Sep 27th, 2009
Try
ascii += cs_string
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: DEATHMASTER is an unknown quantity at this point 
Solved Threads: 0
DEATHMASTER DEATHMASTER is offline Offline
Newbie Poster

Re: Python code help

 
0
  #3
Sep 27th, 2009
Tried that but it only shifts in the other direction, which I definitely don't want.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Python code help

 
0
  #4
Sep 27th, 2009
Why don't you try something like this:
  1. from string import letters
  2. # Upper letters only
  3. letters = letters[26:]
  4.  
  5. c_string = raw_input("Enter desired sentence to convert: ")
  6.  
  7. cs_string = 5
  8. ci_string = cs_string % 26
  9. upper_string = c_string.upper()
  10.  
  11. for ch in upper_string:
  12. lett_idx = letters.find(ch)
  13.  
  14. if lett_idx != -1:
  15. lett_idx -= cs_string
  16. if lett_idx < 0:
  17. lett_idx += len(letters)
  18.  
  19. print letters[lett_idx],

You don't actually need to do the check for if lett_idx < 0 since negative indices in Python will still give you the same result, I was just keeping your logic intact
Last edited by jlm699; Sep 27th, 2009 at 7:30 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: DEATHMASTER is an unknown quantity at this point 
Solved Threads: 0
DEATHMASTER DEATHMASTER is offline Offline
Newbie Poster

Re: Python code help

 
0
  #5
Sep 27th, 2009
Hmm, haven't learned any of that stuff, I think I should keep to modulus, any idea on how to use it? Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: DEATHMASTER is an unknown quantity at this point 
Solved Threads: 0
DEATHMASTER DEATHMASTER is offline Offline
Newbie Poster

Re: Python code help

 
0
  #6
Sep 27th, 2009
Hey guys I managed to figure it out myself. Thanks for the help.
However what in my code makes it insert a space after every letter and how do I undo that? like if I input "BJ" the output is "W E" and I just want "WE." And how can I restrict it to an 80 character window without wrapping if the input is a very long single line? Thanks a ton.
Last edited by DEATHMASTER; Sep 27th, 2009 at 10:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Python code help

 
0
  #7
Sep 28th, 2009
Originally Posted by DEATHMASTER View Post
Hey guys I managed to figure it out myself. Thanks for the help.
However what in my code makes it insert a space after every letter and how do I undo that? like if I input "BJ" the output is "W E" and I just want "WE." And how can I restrict it to an 80 character window without wrapping if the input is a very long single line? Thanks a ton.
Having a comma in your print statement gives you the spaces. Either upgrade to Python3.X with the new print function or use sys.stdout like so:
  1. import sys
  2.  
  3. # Your code here
  4.  
  5. #Instead of print chr(ascii), use this:
  6. sys.stdout.write(chr(ascii))

Either that or simply create a new string variable in your loop and print it after the loop exits:

  1. new_str = ''
  2.  
  3. # Do stuff
  4.  
  5. for ch in usr_input:
  6. # Do stuff
  7. new_str += new_character
  8.  
  9. print new_str
HTH
Last edited by jlm699; Sep 28th, 2009 at 2:48 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC