Error: the encoded message must be a list of integers

Question:

I am creating a prime encryption program however I am running into a very perplexing error.

This is my code:

import random
import datetime

def decode(encoded_message, p):
    # Initialize an empty list to store the decoded message
    decoded_message = []
    # Iterate over the encoded characters in the encoded message
    for c in encoded_message:
        # Decode each character by subtracting the prime number p from its ASCII value
        decoded_c = chr(int(c) - p)
        # Append the decoded character to the decoded message
        decoded_message.append(decoded_c)
    # Return the decoded message as a string
    return ''.join(decoded_message)
def is_prime(n, k=20):
  # If n is less than 2, it is not prime
  if n < 2:
    return False

  # If n is 2 or 3, it is prime
  if n == 2 or n == 3:
    return True

  # If n is even, it is not prime
  if n % 2 == 0:
    return False

  # Find r and s such that n - 1 = 2^s * r, where r is odd
  s = 0
  r = n - 1
  while r % 2 == 0:
    s += 1
    r //= 2

  # Repeat k times:
  for _ in range(k):
    a = random.randrange(2, n - 1)
    x = pow(a, r, n)
    if x != 1 and x != n - 1:
      j = 1
      while j < s and x != n - 1:
        x = pow(x, 2, n)
        if x == 1:
          return False
        j += 1
      if x != n - 1:
        return False
  return True
# Get the key from the user
key = int(input("What is the key?"))

# Check if the key is prime
if not is_prime(key):
  print("Error: the key must be a prime number")
else:
  # Get the encoded message from the user
  encoded_message = list(input("What is the encoded message?"))

  # Check if the encoded message is a list of integers
  if not all(isinstance(i, int) for i in encoded_message):
    print("Error: the encoded message must be a list of integers")
  else:
    # Decode the message
    decoded_message = decode(encoded_message, key)
    print("Your decoded message is:", decoded_message)


  

What I enter is
[72, 77, 82, 77, 82, 84, 83, 80, 87, 82, 76, 73, 85]
into encoded_message = list(input("What is the encoded message?"))
I also enter 5 into key = int(input("What is the key?"))
The key input works just fine. I do not know what the problem is with this. I have tried removing brackets and removing the commas, but nothing seems to work…

No matter what I change, I get this error:
Error: the encoded message must be a list of integers

Asked By: s j

||

Answers:

In

encoded_message = list(input("What is the encoded message?"))

list(input(...)) takes the string output by input and turns it into a list, so without an input:

>>> list("[1,2,3]")
['[', '1', ',', '2', ',', '3', ']']

The result is obviously not what you want, but use ast.literal_eval instead to have Python parse (some limited) Python syntax so you get an actual list of ints:

>>> import ast
>>> ast.literal_eval("[1,2,3]")
[1, 2, 3]
Answered By: AKX
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.