1

So a little while ago I asked for some help with an encryption program,
And you guys were amazing and came up with the solution.
So I come to you again in search of help for the equivalent decryption program. The code I have got so far is like this:

whinger = 0
bewds = raw_input ('Please enter the encrypted message: ')
bewds = bewds.replace(' ', ', ')
warble = [bewds]
print warble
wetler = len(warble)
warble.reverse();
while whinger < wetler:
    print chr(warble[whinger]),
    whinger += 1

But when I input
101 103 97 115 115 101 109
it comes up with the error that the input is not an integer.
What I need is when I enter the numbers it turns them into a list of integers.
But I don't want to have to input all the numbers separately.

Thanks in advance for your help :P

4 Answers 4

2

To convert input string into a list of integers:

numbers = [int(s) for s in "101 103 97 115 115 101 109".split()]
Sign up to request clarification or add additional context in comments.

3 Comments

He is also adding ','. So he might need to do a split(', ') or just stop adding commas.
@aychedee: split(", ") won't work if there is no space after comma and it won't split integers separated by whitespace. @martineau's answer with replace(",", " ").split() could be used to support both spaces and commas. Though I'd limit valid input only to spaces for simplicity.
Yeah, but he was explicitly replacing spaces with , . (comma, space) So :-P
1

Here's almost the simplest way I can think of to do it:

s = '101 103 97 115 115 101 109'
numbers = []
for number_str in s.replace(',', ' ').split():
    numbers.append(int(number_str))

It will allow the numbers to be separated with commas and/or one or more space characters. If you only want to allow spaces, leave the ".replace(',', ' ')" out.

Comments

0

Your problem is, that raw_input returns a string to you. So you have two options.

1, Use regular expression library re. E.G.:

import re
bewds = raw_input ('Please enter the encrypted message: ')
some_list = []
for find in re.finditer("\d+", bewds):
    some_list.append(find.group(0))

2, Or you can use split method as described in the most voted answer to this question: sscanf in Python

2 Comments

Sure, that's why I have mentioned it :-) But asking guy is obviously new in python so a little more options, how to do that, won't do any harm :-)
IMHO, someone new should be shown the simplest way(s) to do it first.
0

You could also use map

numbers = map(int, '101 103 97 115 115 101 109'.split())

This returns a list in Python 2, but a map object in Python 3, which you might want to convert into a list.

numbers = list(map(int, '101 103 97 115 115 101 109'.split()))

This does exactly the same as J. F. Sebastian's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.