0

I am trying to display an encrypted message by converting the characters in a string within a text file into their equivalent ASCII values. Once this is done, I would like to add an offset factor, say 10 and then convert back and display the message. Furthermore, I would also like it if somebody could overlook the spaces, meaning they do not get encrypted.Finally, a bonus would be if somebody could deduct 94 from the ASCII value if it's higher than 126.

So far, I've "generated" the integers, but all I all I get is square brackets

user_input = input("Enter a text file name. Remeber to add 'txt'")
file = open(user_input,"r+") 
print(file.readline())

int_text = ([ord(c) for c in file.readline()])
print(int_text)

Thanks for the help.

3
  • 1
    does it actually open the file? you should use raw_input instead of input Commented Jun 28, 2015 at 10:03
  • 2
    This looks like Python 3, so no, they shouldn't. Commented Jun 28, 2015 at 10:06
  • I'm voting to close this question as off-topic because this is neither a code-writing nor tutorial service Commented Jun 28, 2015 at 10:33

1 Answer 1

1

shifting is taking a text and an optional offset factor and returns the transformed string:

def shifting(text, n=10):
    ints = (ord(c) for c in text)
    def helper():
        for c in ints:
            if c==32:
                yield " "
            elif c+n > 126:
                yield chr(c+n-94)
            else:
                yield chr(c+n)
    return "".join(helper())

enc_text = shifting(file.readline(),10)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.