[Python] Problem regarding encoding non-string lists into foreign text using mapping method

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    So the other day I was trying to encode a program that would both encode and decode my normal english input into an jumbled and encoded text (Ex: hello world => X8IIe PexIa) where I could send it to someone else and they could decode it to get the answer. In order to create the custom language, I referred to mapping and could map singular exchanges (t turns into X, g into # and vice versa), but thought it was too simple to decode and tried to randomize the decoding using a list, with e having the probability of being any instance in the list, such as a being randomized into either ["f","g","a"].

    The function I was trying to use is:

    def custom_encode(text, mapping):
    encoded_text = []
    for char in text:
    if char.lower() in mapping:
    code = mapping[char.lower()]
    if char.isupper():
    code += 'U' # Append 'U' for uppercase
    encoded_text += code
    else:
    encoded_text += char
    return encoded_text


    And the int just won't work, I was stuck in the encoding part, as I couldn't figure out a way to get around the int factor in mapping. Every time I try to extract the original text I fail and this message pops up:

    Traceback (most recent call last):
    File "/Users/feedable_dolphin/Documents/Python/Hakao.py", line 119, in <module>
    encoded_message = custom_encode(message, letter_to_number)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Users/feedable_dolphin/Documents/Python/Hakao.py", line 10, in custom_encode
    encoded_text += code
    TypeError: 'NoneType' object is not iterable


    Is there any way to get around the NoneType and iterate the code?

    For reference, I created a big list in different languages as I thought the 26 alphabet was too easy to decode if I just used iterations of english. So I downloaded the weirdest keyboard input I could find and created this monstrous list:

    list1 = ["ճ","Ճ","U","ރ","","","","१","२","३" ]
    list2 = ["ւ","Ա","J","ފ","ޅ","","","५","४","६" ]
    list3 = ["ե","Զ","ô","ވ","له","","","","७","८","١" ]
    list4 = ["ր","Ւ","M","ތ","૧","","","र","٢","٣" ]
    list5 = ["տ","Ս","I","ގ","૨","","","٤","٥","٦" ]
    list6 = ["յ","Խ","K","ބ","૩","","","","٧","٨","٩" ]
    list7 = ["ւ","Ե","ł","ޔ","૪","","","","ق","و","ە" ]
    list8 = ["ի","Դ","O","ހ","૫","","","","ر","ت","ی" ]
    list9 = ["ո","Ծ","Q","ނ","૬","ا","س","د","ف","گ","ه" ]
    list10 = ["պ","Ր","A","ު","૭","ز","ا","ش","س","خ","ꤵ" ]
    list11 = ["ա","Ֆ","ö","ޖ","૮","خ","ز","ع","ꤹ","ꥂ","ꤷ" ]
    list12 = ["ս","Վ","দ","މ","૯","ج","ꤺ","ꥁ","ꤱ","ꤲ","ꤴ" ]
    list13 = ["դ","Տ","Z","ި","૦","ڤ","ꤳ","ې"]
    list14 = ["ֆ","Գ","E","ކ","N","ب","ꤿ","ۋ"]
    list15 = ["գ","Բ","D","ޮ","H","ن","ر",]
    list16 = ["հ","Յ","C","ލ","Y","م","ي","ت","ۇ","พ","ไ" ]
    list17 = ["ձ","Հ","খ","ޕ","B","પૅ","،","ھ","س","ะ","ส" ]
    list18 = ["կ","Ն","R","ޤ","G","ق","د","ا","ە","ๆ","ด" ]
    list19 = ["լ","Ի","F","এ","જ","د","ق","ى","ك","ป","แ" ]
    list20 = [";","Կ","ľ","ާ","स","અ","ص","ل","ز","ฏ","?" ]
    list21 = ["զ","Ո","V","ޡ","क","લ","ر","ش","غ","ۈ","ญ" ]
    list22 = ["խ","Լ","T","ޢ","प","લૅ","ف","ب","ن","م","ק" ]
    list23 = ["ծ","Պ","ް","ޠ","ށ","બ","ذ","ש","אַ","ז","ฒ" ]
    list24 = ["վ","ঔ","ަ","ޜ","ޛ","म","ꤼ","ע","ס","כ","ฤ" ]
    list25 = ["բ","ও","ޒ","ޥ","ޥ","दु","ꥆ","ר","ד","צ","ฆ" ]
    list26 = ["ն","ļ","އ","ޓ","स","द","ꤴ","ט","פֿ","צ","ฎ" ]
    list27 = ["մ","ৰ্","ސ","ޣ","","","ꤲ","ײַ","ג","װ","๐" ]
    list28 = ["গ","L","ޱ","ޝ","","","ꤱ","ו","ה","ב","ฯ" ]
    list29 = ["১ু","জ","ެ","","","ꥁ","י","ײ","נ" ]
    list30 = ["হ","ދ","ޗ","ޚ","","","ꤺ"]


    Then after that I proceeded to random the list and give each one a designation to a letter of the alphabet.

    def Dice(listabc):
    random.choice(listabc)

    letter_to_number = {'a': Dice(list1),
    'e': Dice(list2),
    'i': Dice(list3),
    'o' :Dice(list4),
    'u' :Dice(list5),
    'q': Dice(list6),
    'w' : Dice(list7),
    'r': Dice(list8),
    't': Dice(list9),
    'y': Dice(list30),
    'p': Dice(list10),
    's': Dice(list11),
    'd': Dice(list12),
    'f': Dice(list13),
    'g': Dice(list14),
    'h': Dice(list15),
    'j': Dice(list16),
    'k': Dice(list29),
    'l': Dice(list17),
    'z': Dice(list18),
    'x': Dice(list19),
    'c': Dice(list26),
    'v': Dice(list20),
    'b': Dice(list21),
    'n': Dice(list27),
    'm': Dice(list22),
    ',': Dice(list23),
    '.': Dice(list28),
    '?': Dice(list24),
    ' ': Dice(list25)}


    And my execute command is below:

    message = input("Enter your message: ")
    encoded_message = custom_encode(message, letter_to_number)
    print(f"Custom encoded: {encoded_message}")


    I've searched everywhere but can't seem to find a problem with mapping word to word and letter to letter, but when it comes to lists the program doesn't execute. Is there a problem with the language interpretation (It's not python can't understand, it prints the languages perfectly) or the code is flawed?

    Please help me make it so that the dice randomizes each letter in my input message into a random variable in my lists.

    Continue reading...

Compartilhe esta Página