views:

241

answers:

4

When I run my program (which decrypts a paragraph from a certain document), I have:

W
E
T
H
E
P
E
O
P
L
E
O
F
T
H
E
U
N
I
T
E
D
S
T
A
T
E
S
I
N
O
R
D
E
R
T
O
F
O
R
M
A
M
O
R
E
P
E
R
F
E
C
T
U
N
I
O
N
E
S
T
A
B
L
I
S
H
J
U
S
T
I
C
E
I
N
S
U
R
E
D
O
M
E
S
T
I
C
T
R
A
N
Q
U
I
L
I
T
Y
P
R
O
V
I
D
E
F
O
R
T
H
E
C
O
M
M
O
N
D
E
F
E
N
S
E
P
R
O
M
O
T
E
T
H
E
G
E
N
E
R
A
L
W
E
L
F
A
R
E
A
N
D
S
E
C
U
R
E
T
H
E
B
L
E
S
S
I
N
G
S
O
F
L
I
B
E
R
T
Y
T
O
O
U
R
S
E
L
V
E
S
A
N
D
O
U
R
P
O
S
T
E
R
I
T
Y
D
O
O
R
D
A
I
N
A
N
D
E
S
T
A
B
L
I
S
H
T
H
I
S
C
O
N
S
T
I
T
U
T
I
O
N
F
O
R
T
H
E
U
N
I
T
E
D
S
T
A
T
E
S
O
F
A
M
E
R
I
C
A

You can't tell from here but basically it is one letter per line.

However, I want it to say:

WE THE PEOPLE OF THE UNITED STATES, IN ORDER TO FORM A MORE PERFECT UNION, ESTABLISH JUSTICE, INSURE DOMESTIC TRANQUILITY, PROVIDE FOR THE COMMON DEFENSE, PROMOTE THE GENERAL WELFARE, AND SECURE THE BLESSINGS OF LIBERTY TO OURSELVES AND OUR POSTERITY, DO ORDAIN AND ESTABLISH THIS CONSTITUTION FOR THE UNITED STATES OF AMERICA.

What is the code I must add to my program in order for this to happen?

+1  A: 

Do you do something like this:

for char in string:
    print char

? If yes, fix that to:

for char in string:
    print char,

the comma(,) at the end of the line omits the newline print normally prints. But even that is probably not what you want (since it prints a space after each char), the following code should fix that, too:

import sys
for char in string:
    sys.stdout.write(char)
Johannes Weiß
The comma at the end of the print statement adds a space to the end of the printed string. To fix this, you will need to add a print "\b" (which moves the cursor 1 space to the left) on the next iteration. Also, you are not checking for the text limit (80 chars), so this will print the entire paragraph on one line
inspectorG4dget
+6  A: 

I don't know what that one-character-a-line is about because you didn't tell us the reason, but the textwrap module will give you what you want:

s="WE THE PEOPLE OF THE UNITED STATES, IN ORDER TO FORM A MORE PERFECT UNION, ESTABLISH JUSTICE, INSURE DOMESTIC TRANQUILITY, PROVIDE FOR THE COMMON DEFENSE, PROMOTE THE GENERAL WELFARE, AND SECURE THE BLESSINGS OF LIBERTY TO OURSELVES AND OUR POSTERITY, DO ORDAIN AND ESTABLISH THIS CONSTITUTION FOR THE UNITED STATES OF AMERICA."

import textwrap
print "\n".join(textwrap.wrap(s, 80))

I reconstructed your original code from your comment, and this is a corrected version:

# You don't even use this so why import it? --> import string

def main():
    user_string = raw_input()
    all_caps = user_string.upper() # guess you wanted to make it uppercase
    output = [] # this will hold the decoded characters

    for char in all_caps:
        if char.isalpha():
            value = ord(char)
            if 70 <= value <= 90: # look at this, almost no other programming language supports that syntax
                num = value - 5
            elif 65 <= value <= 69:
                num = value + 21
            output.append(chr(num)) # add the decoded character to the output list
        else:
            output.append(char) # add the character verbatim to the output list (e.g. whitespace)

    print "".join(output) # print out the list by putting it together into a string

main()
AndiDog
That is awesome. I totally forgot about textwrap.
jathanism
here is my code:import stringdef main(): user_string = raw_input() all_caps = (user_string) for char in all_caps: if char.isalpha(): value = ord(char) if value >= 70: if value <= 90: num = value - 5 if value <= 69: if value >= 65: num = value + 21 print chr(num)main()as you can see I'm using Caesar Cypher to Decrypt a message that reads "WE THE PEOPLE..." but when I run this it doesn't print how I want it
Stephanie
@Andrew: You can't have multiple lines of code in comments. Please edit your answer to include that code!
AndiDog
Sorry, the above didn't turn out how I intended it to...Please scroll down to look as I rewrote this in an answer, thanks!
Stephanie
@Andrew: Don't put it in an answer. There's a link under your question giving you the opportunity to edit!! Nobody will read through all answers to find out that you posted an answer to your own question containing hints on how to solve the mystery.
AndiDog
@AndiDog: Oh...I didn't know that -_-. You can tell I'm new to this website haha. But did you get the edits in the answer anyway?
Stephanie
@AndiDog: Oh wait...I see that you edited your question! I will see if this works. Thank you for everything!
Stephanie
@AndiDog: WOW! You are a life-saver haha. Now how do I give you a gold badge?
Stephanie
@Andrew: Asking professional Python programmers is not cheating. Copying my code and handing it in would be cheating (and plagiarism, at least that's what your professor will be thinking). And no, I won't remove that answer. And your question won't be removed either. It's like on Wikipedia, all the old versions remain and you can't really delete it. You should be glad if nobody sends this to failblog or reddit.
AndiDog
A: 
print s[0],
i = 1
for char in s[1:]:
    if i%80:
        print "\bchar",
    else:
        print "\b\n"
    i += 1
inspectorG4dget
A: 

THANK YOU ANDIDOG :)

Stephanie
Responses to someone's answer belong as comments on that answer, not new answers.
MatrixFrog