Mar-31-2024, 11:59 AM
Some tips and as example print both with and without spaces.
Also
So just iterate over the string,no adding on is needed.
Also
range(len(chain_to_code))
is not good here,just remember that range(len(sequence)) is almost always a bad way.So just iterate over the string,no adding on is needed.
ALPHABET = "abcdefghijklmnopqrstuvwxyz" GAP = 4 chain_to_code = "ave caesar" encoded_without_spaces = "" encoded_with_spaces = "" for character in chain_to_code: #print(repr(character)) # Tips to see all if character in ALPHABET: encoded_char = ALPHABET[(ALPHABET.index(character) + GAP) % len(ALPHABET)] encoded_without_spaces += encoded_char encoded_with_spaces += encoded_char # Handle spaces elif character == " ": encoded_with_spaces += " " print(f"Encoded without spaces: {encoded_without_spaces}") print(f"Encoded with spaces: {encoded_with_spaces}")
Output:Encoded without spaces: ezigeiwev
Encoded with spaces: ezi geiwev
But just need this as it will handle inputs string with or without space correct.ALPHABET = "abcdefghijklmnopqrstuvwxyz" GAP = 4 chain_to_code = "ave caesar" concatenated_chain = "" concatenated_chain_with_spaces = "" for character in chain_to_code: if character in ALPHABET: concatenated_chain += ALPHABET[(ALPHABET.index(character) + GAP) % len(ALPHABET)] else: concatenated_chain += character print(f"Encoded: {concatenated_chain}")
Output:Encoded: ezi geiwev