-3

How do I make my program print the answers on separate lines + with what key the line corresponds to?

def break_crypt(message):
    for key in range(1,27):

        for character in message:

            if character in string.uppercase:

                old_ascii=ord(character)
                new_ascii=(old_ascii-key-65)%26+65
                new_char=chr(new_ascii)
                sys.stdout.write(new_char),

            elif character in string.lowercase:

                old_ascii=ord(character)
                new_ascii=(old_ascii-key-97)%26+97
                new_char=chr(new_ascii)
                sys.stdout.write(new_char),

            else:
                sys.stdout.write(character),

2 Answers2

1

to jump a line simply use "\n" for instance:

sys.stdout.write("a\nb")

will write a and b in differents lines

use + to add a string to another

sys.stdout.write("a"+variable+"b")

there is other "more advanced" ways like

sys.stdout.write("a%sb" % variable)

or

sys.stdout.write("a{0}b".format(variable)

also in your code if there is no point of using sys.stdout.write don't use it

this may helps you https://docs.python.org/2/tutorial/introduction.html

Arnaud Aliès
  • 1,079
  • 13
  • 26
-2

If you simply add the following at the end of the outer loop, then it'll both print the key and go to the next line:

        print '', key

Then the output will look like this:

Sghr hr z sdrs 1
Rfgq gq y rcqr 2
Qefp fp x qbpq 3
        .
        .
        .
Uijt jt b uftu 25
This is a test 26

But I would really build the whole string for the current key in a string variable and then print it at once.

Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107