簡體   English   中英

+ 不支持的操作數類型:'int' 和 'function'

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

好的,所以我需要加密或解密給定整數列表、消息和密鑰流值的消息。 每當我運行我的函數時,我都會收到此錯誤:

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'

這些是我認為涉及的兩個功能:

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

有誰知道為什么會出現這個問題?

看起來問題是get_next_keystream_value沒有在任何地方定義。

目前尚不清楚它是否意味着您尚未初始化的變量,或者它是否應該是尚未編碼的函數的函數調用,在這種情況下它應該是get_next_keystream_value() 鑒於您的代碼結構,您似乎試圖將其作為key_value ,在這種情況下,您需要key_value其初始化為整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM