1
votes

def doppelt(word):

word = word.lower()
encoded = ''
for c in word:
    new_ord = word
    new_ord = 96 + (new_ord - 121) % 26
    encoded += chr(new_ord)
return encoded

TypeError
Traceback (most recent call last) in ()

----> 1 doppelt('string')

in doppelt(word) 4 new_ord = word 5 for c in word: ----> 6 new_ord = 96 + (new_ord - 121) % 26 7 encoded += chr(new_ord) 8 return encoded

TypeError: unsupported operand type(s) for -: 'str' and 'int'

3

3 Answers

0
votes

you can convert your character to integer by

ord(c)

so the loop will look like this

for c in word:
    new_ord = ord(c)
    new_ord = 96 + (new_ord - 121) % 26
    encoded += chr(new_ord)
0
votes

Addition of an Integer with a String or Char raises the TypeError exception in Python.

So you first need to convert your new_ord to a integer first, before adding. You, can do this by the ord() function.

0
votes

You cannot perform - operation (substraction) between a number (type: int) and a character (type: str).

For instance in

... new_ord - 121 ...

What you need to do is to first convert the character in to its equivalent integer using the in-built function ord which does just that.

See link: https://docs.python.org/library/functions.html#ord

SIDENOTE:

in line 4

   new_ord = word

I think it should actually be new_ord = c, which now become new_ord = ord(c).