Reputation: 11
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
Upvotes: 1
Views: 837
Reputation: 123393
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.
Upvotes: 1
Reputation: 32300
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.
Upvotes: 0
Reputation: 3579
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
Upvotes: 0
Reputation: 414079
To convert input string into a list of integers:
numbers = [int(s) for s in "101 103 97 115 115 101 109".split()]
Upvotes: 2