Monday, January 7, 2019

codingdirectional: Encrypt the text with python

Hello again, today let us continue to explore the question on the previous website which allows us to develop a game with python. In this chapter, we are going to create a function which will encrypt the text passes in as the first argument based on the integer passed in as the second argument to that same function. This function will only accept the lowercase character from a-z and the empty space. What this function does is to add up the character code of an a-z character with the second argument of that function to produce a new character and finally return the entire new text based upon this encrypt method.

from string import punctuation

def to_encrypt(text, delta):

    to_list = list(text)
    new_text = ''
    
    for item in to_list:
        
        if(item not in punctuation):
            if(item.isspace()):
                new_text += ' '
            else:
                if(delta > 0):
                    new = (ord(item) + delta + 26)
                    if(new > 122):
                        new = new - 122 + 96
                    new_text += chr(new)
                else:
                    new = (ord(item) + delta)
                    
                    if(new > 122):
                        new = new - 122 + 96
                    new_text += chr(new)
            
    return new_text

The above method will take care of the overflow issue where if the character code has get passed the a-z character code range after the summation process then the difference between the maximum/minimum value of character code and the overflow value will be used to recalculate the new character position. The empty space in the text will be left alone.

I do wish you like this one, we will continue to solve more problems together on this site and the other site as well in the coming article.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...