943,733 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 408
  • Python RSS
Sep 27th, 2009
0

Python code help

Expand Post »
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?
python Syntax (Toggle Plain Text)
  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),
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DEATHMASTER is offline Offline
18 posts
since Sep 2009
Sep 27th, 2009
0

Re: Python code help

Try
ascii += cs_string
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 27th, 2009
0

Re: Python code help

Tried that but it only shifts in the other direction, which I definitely don't want.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DEATHMASTER is offline Offline
18 posts
since Sep 2009
Sep 27th, 2009
0

Re: Python code help

Why don't you try something like this:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Sep 27th, 2009
0

Re: Python code help

Hmm, haven't learned any of that stuff, I think I should keep to modulus, any idea on how to use it? Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DEATHMASTER is offline Offline
18 posts
since Sep 2009
Sep 27th, 2009
0

Re: Python code help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DEATHMASTER is offline Offline
18 posts
since Sep 2009
Sep 28th, 2009
0

Re: Python code help

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:
python Syntax (Toggle Plain Text)
  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:

python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Homework Help, new programmer!
Next Thread in Python Forum Timeline: log file segmentation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC