1

In my sentence i want :

  • replace every # by *
  • skip every ?
  • if the ascii value of a char is pair so i am transforming into LowerCase + Shift Value ( input user)
  • if the ascii value of a char is impair so i am transforming into UpperCase + Shift Value ( input user) Its tell me

line 19, in replace new = ord(lower(c)) + (shift % 26) TypeError: ord() expected string of length 1, but NoneType found

    def lower(c):
    if 'A' <= c <= 'Z':
        c = chr(ord(c) + (ord('a') - ord('A')))


def upper(c):
    if 'a' <= 'c' <= 'z':
        c =  chr(ord(c) - (ord('a') - ord('A')))


def replace(text):
    new_text = ''
    for c in text:
        if c == '#':
            new_text += '*'
        elif c == '?':
            continue
        elif ord(c) % 2 == 0:
            new = ord(lower(c)) + (shift % 26)
            if new > ord('z'):
                new = (ord('a') + (new - ord('z')))
            new_text += chr(new)

        elif ord(c) % 2 != 0:
            new = ord(upper(c)) + (shift % 26)
            if new_text > 'Z':
                new_text = chr('Z' + (new_text - 'Z'))
            new_text += chr(new)

    return new_text


sentence = (input("Please enter a sentence to encrypt:\n"))
shift = int(input("Please enter the size of the shift:\n"))
sentence = replace(sentence)

  • `c` isn't between `A` and `Z` so your `lower` function does not return anything – Sayse Nov 25 '19 at 13:38
  • Does this answer your question? [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – Sayse Nov 25 '19 at 13:38
  • There is also a typo in your `upper` function so that won't work as you expect either – Sayse Nov 25 '19 at 13:39
  • How should i write my Upper/Lower function so ? –  Nov 25 '19 at 13:43
  • You need to decide what you expect to happen when your if statement isn't entered, see the duplicate. – Sayse Nov 25 '19 at 13:44
  • @Sayse I change so the functio change directly the value of the char but does not seem to help :/ –  Nov 25 '19 at 13:48

0 Answers0