| | |
Python code help
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 18
Reputation:
Solved Threads: 0
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)
c_string = raw_input("Enter desired sentence to convert: ") cs_string = 5 ci_string = cs_string % 26 upper_string = c_string.upper() for ch in upper_string: ascii = ord(ch) if ch.isalpha(): ascii -= cs_string if ascii > ord("z"): ascii -= 26 print chr(ascii),
Why don't you try something like this:
You don't actually need to do the check for
python Syntax (Toggle Plain Text)
from string import letters # Upper letters only letters = letters[26:] c_string = raw_input("Enter desired sentence to convert: ") cs_string = 5 ci_string = cs_string % 26 upper_string = c_string.upper() for ch in upper_string: lett_idx = letters.find(ch) if lett_idx != -1: lett_idx -= cs_string if lett_idx < 0: lett_idx += len(letters) 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.
•
•
Join Date: Sep 2009
Posts: 18
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
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.
python Syntax (Toggle Plain Text)
import sys # Your code here #Instead of print chr(ascii), use this: 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)
new_str = '' # Do stuff for ch in usr_input: # Do stuff new_str += new_character print new_str
Last edited by jlm699; Sep 28th, 2009 at 2:48 am.
![]() |
Similar Threads
- Display Python code on Blogspot (Python)
- Python code to 'C' conversion (Python)
- Twill - using 'find' command from python code (Python)
- Running Python Code in a Webpage (Python)
- python code security or set up a complied python code with a password (Python)
- Help with Python code and accessing ZODB (Python)
Other Threads in the Python Forum
- Previous Thread: Homework Help, new programmer!
- Next Thread: log file segmentation
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app beginner cipher client code conversion coordinates curves development dictionary dynamic examples excel feet file float font format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain maze microcontroller millimeter mouse mysqldb number numbers output parsing path permissions port prime program programming projects py2exe pygame pymailer pyqt python queue random raw_input recursion recursive scrolledtext searchingfile shebang slicenotation socket split ssh string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables vigenere web windows wx.wizard wxpython xlwt






