Update using passed parameters instead of global variables.

This commit is contained in:
Josh Mudge 2018-10-06 11:10:52 -06:00
parent baa81c46d1
commit 024c033ac2
2 changed files with 34 additions and 33 deletions

View File

@ -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)

9
passing-parms.py Normal file
View File

@ -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")