954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python code help

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?

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),
DEATHMASTER
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Try
ascii += cs_string

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Tried that but it only shifts in the other direction, which I definitely don't want.

DEATHMASTER
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Why don't you try something like this:

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

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

Hmm, haven't learned any of that stuff, I think I should keep to modulus, any idea on how to use it? Thanks.

DEATHMASTER
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
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.

DEATHMASTER
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
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.


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:

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:

new_str = ''

# Do stuff

for ch in usr_input:
    # Do stuff
    new_str += new_character

print new_str

HTH

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You