Kurnia Rocki Kurnia Rocki - 1 month ago 8
Python Question

Encrypt Program error

I am writing an encrypt program in python in which I need to change the input string ex: HELLO and the output: LIPPS. I am having a problem when I type more one word.

def encr_ypt(s, n):
word=ord(s)
for i in range(len(s)):
if word >= 90 and word <= 97:
hsl = chr(63+n)
if word >= 122:
hsl = chr(95+n)
else:
hsl = chr(word+n)
return hsl

st=raw_input('input string : ')
print encr_ypt(st, 4)

Answer

The ord(i) fucntion takes a single character and then returns the ASCII value of resulting character. Your code is trying to get the ASCII value of a string , which returns an error.

Also since you are using hsl variable to store the encrypted string , be sure to use the += operator which adds the resulting character to the string.

Here is the corrected Code

def encr_ypt(s, n):
    hsl = ''
    for word in s:
        word = ord(word)
        if word >= 90 and word <= 97:
            hsl += chr(63+n)
        if word >= 122:
            hsl += chr(95+n)
        else:
            hsl += chr(word+n)
    return hsl

st=raw_input('input string : ')
print encr_ypt(st, 4)