1

My code takes a line of text from the user and attempts to encode or decode the text using a function I have created. However I am trying to print all the results of one line and also include the spaces that the user inputted so that it clearly shows that each word has been encoded. It currently just prints all the results underneath one another and does not include the spaces that the user inputted.

print ""

# To God be the Glory

text = raw_input("Please enter a line of text: ")
text = text.lower()

print ""
key = int(input("Please enter a key: "))

def ascii_func (text) :

for charc in text:
    if charc in ['-', '+', '*', '/', '!' , "@"]:
        print "Error input is not correct"

for charc in text:

    if charc != " " :
       charc = ord(charc)
       charc = (charc - 97) + key
       charc = (charc % 26)
       charc = charc + 97
       charc = chr(charc)
       print charc

ascii_func(text)   
jayoguntino
  • 133
  • 1
  • 7
  • What is this line for? `if charc != " " :` – Chris Martin Aug 18 '15 at 22:10
  • Make sure to indent the logic you want to be a part of ascii_func. Prevent the newlines by importing sys then replacing print charc with sys.stdout.write(charc) then unindent that line to take it out of the if conditional. – jwde Aug 18 '15 at 22:20

1 Answers1

1

Build a string instead of printing one character at a time:

result = ''
for charc in text:
    if charc != " " :
        charc = ord(charc)
        charc = (charc - 97) + key
        charc = (charc % 26)
        charc = charc + 97
        charc = chr(charc)
    result += charc
print result
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97