简体   繁体   中英

unsupported operand type(s) for +: 'int' and 'function'

Ok so I need to encrypt or decrypt a message given a list of integers, a message, and a keystream value. Whenever I run my function I get this error:

File "C:\Users\v\Desktop\Assignment 1\cipher_functions.py", line 152, in <module>
result += decrypt_letter(i, get_next_keystream_value)
File "C:\Users\v\Desktop\Assignment 1\cipher_functions.py", line 51, in <module>
key_stream = (ord_char - key_value) builtins.TypeError: unsupported operand type(s) for -: 'int' and 'function'

These are the 2 functions which I believe are involved:

def encrypt_letter(my_char, key_value):
    '''(str, int) -> str
    '''
    my_list = ['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']
    ord_char = (ord(my_char.upper()) - 65)
    key_stream = (ord_char + key_value)

    if key_stream > 25:
        result = (key_stream - 26)
    elif key_stream <= 25:
        result = key_stream

    key_streamed_value = my_list[result]

    return key_streamed_value

def decrypt_letter(my_upper_char, key_value):
    '''(str, int) -> str
    '''
    my_list = ['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']    
    ord_char = (ord(my_upper_char) - 65)
    key_stream = (ord_char - key_value)

    if key_stream < 0:
        result = (key_stream + 26)
    elif key_stream >= 0:
        result = key_stream

    key_streamed_value = my_list[result]

    return key_streamed_value


def process_message(deck_cards, my_message, convert):
    '''(list of int, str, str) -> str
    '''
    result = ""
    new_message = clean_message(my_message)
    for i in new_message:
        if convert == 'e':
            result += encrypt_letter(i, get_next_keystream_value)
        elif convert == "d":
            result += decrypt_letter(i, get_next_keystream_value)
        return result

Does anyone know why this problem is occurring?

It looks like the problem is that get_next_keystream_value is not defined anywhere.

It is unclear if it is meant to be a variable which you have not initialized, or if it should be a function call for a function that has not been coded yet, in which case it should be get_next_keystream_value() . Given the structure of your code, it looks like you are trying to pass it as the key_value , in which case you need to initialize it to an integer first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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