0

My code:

def Encryption(text):
   for I in text:
      string = ""
      ASCII = ord(text)
      Result = ASCII + Offset
      if Result > 126:
         Result -= 94
      else:
         Result = Result
      ResultASCII = chr(Result)
      string += ResultASCII

For my first piece of GCSE coursework, we had to make an encryption program. The final part that we have to make is the part that actually encrypts your message. I've used this code, however it comes up with an error of:

TypeError: ord() expected a character, but string of length # found

How do I get it to detect a string instead of just a character?

2 Answers 2

2

ord argument is not string, it must be single character:

>>> ord('a')
97

you can try something like this to loop over string:

>>> a = 'hello'
>>> [ ord(x) for x in a ]
[104, 101, 108, 108, 111]

replace this :

ASCII = ord(text)

to this:

ASCII = ord(I)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but how would I fit this into my code? I tried doing ASCII = ord(x) for x in a but it didn't work.
Sorry if I've been unclear, this code is part of my GCSE coursework and it's basically to encrypt a message using the ASCII chart, then giving an 8-character key to the user so they can decrypt it in future.
0
ord: (c)                                                                                                             │
│ ord(c) -> integer                                                                                                    │                                                                                                                     
│ Return the integer ordinal of a one-character string.    

>>> ord('ab')

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found

>>> ord('a')
97

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.