From 024c033ac2a56097c7b2dbddbd75a233100da96f Mon Sep 17 00:00:00 2001 From: Josh Mudge Date: Sat, 6 Oct 2018 11:10:52 -0600 Subject: [PATCH] Update using passed parameters instead of global variables. --- Caeser-Cipher/caeser-cipher.py | 58 +++++++++++++++------------------- passing-parms.py | 9 ++++++ 2 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 passing-parms.py diff --git a/Caeser-Cipher/caeser-cipher.py b/Caeser-Cipher/caeser-cipher.py index 52faac4..23496cd 100644 --- a/Caeser-Cipher/caeser-cipher.py +++ b/Caeser-Cipher/caeser-cipher.py @@ -1,4 +1,4 @@ -# Python Caeser Cipher Helper v1.0.2 +# Python Caeser Cipher Helper v1.1.0 # Usage (encrypt): python3 caeser-cipher.py encrypt secretmessage # Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage @@ -19,19 +19,14 @@ shiftNum = input("Please enter the shift you would like to use (Just hit enter i ciphertext = re.sub (r'([^a-zA-Z]+?)', '', ciphertext) # Remove all non-letters. ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/ letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet -letterCount = 0 # Set variable for keeping track of which letter in the ciphertext we're on. -letterPos = 0 # Set variable for keeping track of the letter's position in the alphabet. -answer = "" # The solution. -commonletter = 0 -def encrypt(): - # Grab global variables. - global ciphertext - global shiftNum - global letters - global letterCount - global letterPos - global answer +def encrypt(ciphertext, shiftNum, letters): # Grab parameters + + # Create local variables for this function. + + letterCount = 0 + letterPos = 0 + answer = "" for c in ciphertext: # For every letter in the ciphertext while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue. @@ -56,16 +51,13 @@ def encrypt(): print("\nYour enciphered text is: " + answer) -def decrypt(): +def decrypt(ciphertext, shiftNum, letters, commonletter): # Grab parameters - # Grab global variables. - global ciphertext - global shiftNum - global letters - global letterCount - global letterPos - global answer - global commonletter + # Create local variables for this function. + + letterCount = 0 + letterPos = 0 + answer = "" for c in ciphertext: # For every letter in the ciphertext while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue. @@ -90,13 +82,13 @@ def decrypt(): print("\nYour decrypted text is: " + answer) -def freq(): - # Grab global variables. - global ciphertext - global commonletter - global letterPos - global shiftNum - print(shiftNum) +def freq(ciphertext): + # Create local variables for this function. + + commonletter = 0 + letterCount = 0 + letterPos = 0 + answer = "" commonletter = collections.Counter(ciphertext).most_common()[0][0] # Find most common letter and thus the shift https://stackoverflow.com/questions/47251934/how-to-count-the-most-frequent-letter-in-a-string @@ -113,13 +105,13 @@ def freq(): letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.) - decrypt() # Decrypt using the frequency found in here. + decrypt(ciphertext, shiftNum, letters, commonletter) # Decrypt using the frequency found in here. if action == "encrypt": - encrypt() + encrypt(ciphertext, shiftNum, letters) # Start the encrypt() function while passing 3 variables as parameters. if action == "decrypt": if shiftNum == "": - freq() + freq(ciphertext) else: - decrypt() + decrypt(ciphertext, shiftNum, letters, commonletter) diff --git a/passing-parms.py b/passing-parms.py new file mode 100644 index 0000000..116fcd5 --- /dev/null +++ b/passing-parms.py @@ -0,0 +1,9 @@ +def no_side_effects(cities, cows): + print(cities) + cities = cities + ["Birmingham", "Bradford"] + print(cities) + print(cows) + +locations = ["London", "Leeds", "Glasgow", "Sheffield"] + +no_side_effects(locations, "Fred")