0
votes

So I've been working on this for a little bit now and I cant figure it out at the moment, I need to use a function for a for loop to turn a list of numbers into a string. Not entirely sure what I am doing incorrectly. any help would be appreciated. this is the list

[67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]

it looks like it does it but in a line and not in a row like "congrats you have unpacked the message using a loop:" https://i.stack.imgur.com/9ee8k.png

2
Hi Fefe, welcome to the site. Please include your code as text in the question, rather than as a screenshot of your editor. If you can, including the output as text is also strongly preferred (screenshots are fine for graphical output, but for output like you have, text would be better). - Blckknght
Thanks for the input! i will next time first ever post! I appreciate everyone for the help! - Fefe

2 Answers

3
votes

It should be:

def convert2string():
    for i in list1:
        print(chr(i), end="")

convert2string()

Congrats you have unpacked the message using a loop!
1
votes

Here is the sample code snippet that resolves your problem. You can update it by adding more punctuation characters and white space character. Hope this helps you.

data = [67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]

white_spaces=[9,10,13,32]
for i in data:
  if (i >=65 and i <=90)or (i>=97 and i<=122) or (i in white_spaces):
    print("%c"%(i), end='')