I am trying to create a Caesar Cipher in Python.
I have managed to put together my program with the help of some other websites but have hit a bit of a wall.
My code allows me to encrypt a single character and will then ouput this and write it to a file.
I want to apply this function to work with a block of text. I know I could add a for loop to go through each character but was wondering how I would go about creating another function called encryptText that calls my encryptChar function but iterates through each character not just a single one.
Here is my encryptCharacter function

def encryptCharacter(character, key):

    encrypted  =''


    if character.isalpha():
            num = ord(character)
            num += key

            if character.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif character.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26
            encrypted+=chr(num)
    else:
            encrypted+=character


    return encrypted

Well, how do you propose to read the whole file and process every letter without using a for loop ?
I think using a for loop here is indeed the right way to go, and if you want to make a function for the whole text, just put a for loop in that function :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.