I have the code for caesar cipher encryption and I am trying to call the cleanup()
function inside the fancy_caesar()
function in order for the input keyword and message to be 'cleaned up.':
#with just .upper()
my_string = ''
def string():
my_string = input("Enter your string: ")
res = cleanup()
print(res)
def cleanup(self):
res = ''
_char = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
lower_char = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
all_char = _char lower_char
for ch in my_string:
if ch in all_char:
res = ch
return (res.upper())
def swap(x, i, j):
i, j = sorted((i, j))
if i and j in range(len(x)):
return x[:i] x[j] x[i 1:j] x[i] x[j 1:]
else:
return None
def inputs():
x = input('Enter your string: ')
i = int(input('Enter the index of the first letter: '))
j = int(input('Enter the 2nd index of the next letter: '))
print(swap(x, i, j))
#print(swap(x, i, j))
def fancy_caesar(message, keyword, true_false_statement):
count = 0
while count == 0:
true_false_statement = str(input("Type True if you want to encrypt.\nType false if you want to decrypt: "))
if true_false_statement == 'true' or true_false_statement == 'True' or true_false_statement == 't' or true_false_statement == 'T':
count = count 1
elif true_false_statement == 'false' or true_false_statement == 'False' or true_false_statement == 'F' or true_false_statement == 'f':
count = count 1
else:
print('None')
alphabet = 'abcdefghijklmnopqrstuvwxyz'
translated_message = ""
keyword_index = 0
for character in message:
if character in alphabet:
number = alphabet.find(character)
if true_false_statement == 'true' or true_false_statement == 'True' or true_false_statement == 't' or true_false_statement == 'T':
number = number (ord(keyword[keyword_index]) - ord('a'))
elif true_false_statement == 'false' or true_false_statement == 'False' or true_false_statement == 'F' or true_false_statement == 'f':
number = number - (ord(keyword[keyword_index])) - ord('a')
keyword_index = 1
keyword_index = keyword_index % len(keyword)
if number >= len(alphabet):
number = number - len(alphabet)
elif number < 0:
number = number len(alphabet)
translated_message = translated_message alphabet[number]
else:
translated_message = translated_message character
return (translated_message)
def main():
message = input("Enter string you want to encrypt/decrypt: ")
keyword = input('Keyword for encryption: ')
true_false_statement = ''
translated_message = fancy_caesar(message, keyword, true_false_statement)
print(translated_message)
main()
I tried adding self
to the cleanup()
function which did not work at all since I was not given anything but blank in the ouput.
def fancy_caesar(message, keyword, true_false_statement):
message = cleanup(message)
keyword = cleanup(keyword)
count = 0
while count == 0:
I tried calling the cleanup()
within the main()
function and I got the same result.
This could be due to the fact that my_string
isn't called in def string()
Which I ended up excluding it out of the cleanup()
function in order for me to use the function for the fancy_caesar
. Could I get any tips to solve these issues?
The output should result in the input string being all capatalized with no spaces or special characters which is done by the cleanup
function.
CodePudding user response:
cleanup()
should take the string to be cleaned as a parameter.
def cleanup(my_string):
res = ''
_char = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
lower_char = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
all_char = _char lower_char
for ch in my_string:
if ch in all_char:
res = ch
return (res.upper())
Then you use it like this:
some_string = cleanup(some_string)