简体   繁体   English

Function 没有正确调用?

[英]Function isn't called properly?

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.':我有凯撒密码加密的代码,我正在尝试在fancy_caesar() function 中调用cleanup() function,以便“清理”输入关键字和消息。

#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.我尝试将self添加到cleanup() function 中,但它根本不起作用,因为我在输出中除了空白外没有得到任何东西。

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.我尝试在main() function 中调用cleanup() ,我得到了相同的结果。

This could be due to the fact that my_string isn't called in def string()这可能是由于def string()中未调用my_string

Which I ended up excluding it out of the cleanup() function in order for me to use the function for the fancy_caesar .我最终将它排除在cleanup() function 之外,以便我将 function 用于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. output 应该导致输入字符串全部大写,没有空格或特殊字符,这是由cleanup function 完成的。

cleanup() should take the string to be cleaned as a parameter. cleanup()应该将要清理的字符串作为参数。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM