Daniel Johnson
Daniel Johnson

Reputation: 35

'expected a character, but string of length # found' Encryption Program not working

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?

Upvotes: 0

Views: 50166

Answers (2)

Hackaholic
Hackaholic

Reputation: 19733

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)

Upvotes: 2

Vishnu Upadhyay
Vishnu Upadhyay

Reputation: 5061

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

Upvotes: 0

Related Questions