0

Good morning everyone! I am new to programming and am learning python. I am trying to create a function that converts each individual char in string into each corresponding individuals ints and displays them one after another. The first error it generates is "c is not defined".

c=''
def encode(secret_message):
    for c in secret_message:
        int_message=+ord(c)
    return int_message

Example of what I want it to do:
secret_message='You' (this is a string)
return: 89 111 117    (this should be an int, not a list)
note: 'Y'=89, 'o'=111, 'u'=117

the idea is that encode takes in a parameter secret message. It then iterates through each char in c and converts each char from a string to an int. Then it returns the entire message in ints.

I'm also not sure how to get each char to appear in int_message. As of now, it looks like it will add all the ints together. I want it to simply place them together (like a string). Do I need to convert it back to a string after I get the int values then concatenate?

  • 1
    Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – Léopold Houdin Sep 22 '18 at 17:23
  • Y'all need to read the question more carefully. The problem is creating a list of ints, not converting a string to an int. – Aran-Fey Sep 22 '18 at 17:23
  • 2
    It isn't clear to me what you are trying to do. What would be some example input with the corresponding expected output? – juanpa.arrivillaga Sep 22 '18 at 17:24
  • Suppose c='You' then I want to return 89 111 117 – Tiffany J Munn Sep 22 '18 at 17:26
  • [edit] that into the question, please. – Aran-Fey Sep 22 '18 at 17:27
  • @Tiffany J Munn, could you provide in your question the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)." – Artem Sep 22 '18 at 17:30
  • Ok, but what would your output be? Another string? A `list` of `int` objects? – juanpa.arrivillaga Sep 22 '18 at 17:35
  • I added an example input and output. Im not sure the name of it, but i just want the ouput to an int that lists each corresponding int in order. But it should also be an int not a list or string. – Tiffany J Munn Sep 22 '18 at 17:53

3 Answers3

0

Here is an exapmle of how to do this, I would suggest not returning the coded message as a list but you should turn the message into a list to get the individual characters, return it as a string but leave spaces to separate the code or you will have one long number you cant interpret back, then when you decode a message you can enter it as 84 105 102 102 97 110 121 and then split this and decode the individual ints

def encode(s):
    s = [str((ord(i))) for i in list(s)]
    return ' '.join(s)

def decode(s):
    s = [(chr(int(i))) for i in s.split()]
    return ''.join(s)

print(encode('Tiffany'))
print(decode('84 105 102 102 97 110 121'))
~/python/stack/sept/twenty_2$ python3.7 alice.py
84 105 102 102 97 110 121
Tiffany
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

This should do what you want:

def encode(s):
    return int(''.join([str(ord(char)) for char in s]))

Or this:

def encode_2(s):
    return int(''.join(map(str, [ord(char) for char in s])))
Andrew Winterbotham
  • 1,000
  • 7
  • 13
0

I don't know why your getting an error about c not being defined, but there are just a couple of modifications that you need to make to your code.

def encode(secret_message):
    message = ""
    for c in secret_message:
        message += str(ord(c))
    int_message = int(message)
    bin_message = bin(int_message)
    return bin_message
  • You need to start out by setting the message equal to an empty string before you start adding values to it. This is because your not allowed to add values to a variable that does not exist.
  • Use += instead of =+.
  • Before adding ord(c) to the message, it needs to be converted to a string. This is because adding two integers together will perform arithmetic but adding two strings together will smush them (concatenate them) together.
  • From here, we just need to get to convert everything to binary. We can't convert a string directly to binary, so we convert to an integer first, and then to binary. The int and bin function can be used for this.
hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51