


Besides, it gives me an excuse to tell something about arguments. Better to split things up already, keeps things easier.
CAESER CIPHER CODE
Function-creep comes in and all of a sudden you have 300 lines of code in the same function. How about another function? It may be a bit overkill here since the main function I wrote earlier already covers everything and the code only has one purpose, but code usually grows. Since some of those are only read and never overwritten (pseudo- constants), it's usually preferred to CAPITALIZE them. But let's assume for a moment you want to keep them around. I'm not a fan of global variables, so we'll get rid of them later. # all code that was previously behind the mentioned if-statement goes hereĮverything above the if are global variables. How about taking it a step further to clean things up? def main(): This makes sure the code is only running when the file itself is called, not when it's imported by another file. You're wrapping your code quite nicely here: if _name_ = "_main_":

ĭid you know we can do the same with list? list('foo') returns as well. You just have to know where to find it.įor example, take the first function of your program: def charSplit(str): This means a lot of what you want to accomplish is already written, one way or another. One thing to keep in mind is that Python comes batteries included. And a Caeser Cipher is a good place to start since it's complex enough for an absolute beginner while easy enough to understand what goes on, why and when. # Iterates through result and adds each string to the empty string, "finalResult". # Checks if the string index is equal to the letter index.Įlif charSplit(s).lower() = letters: # Adds a space to the result variable if the string index is a space. # Checks if the string has been converted to the cipher. # Returns a list of each char in the given string. I'm quite new to Python and I'm not very good at explaining.Ĭan this be code be improved? # Imports the lowercase alphabet as a whole string. Sorry if the comments on the code don't explain it well. This code basically just does a Caesar cipher on the contents of the variable "s" with a shift of 1.
