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

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
Barney
  • 11
  • 5

4 Answers4

2

To convert input string into a list of integers:

numbers = [int(s) for s in "101 103 97 115 115 101 109".split()]
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • He is also adding `','`. So he might need to do a `split(', ')` or just stop adding commas. – aychedee Feb 05 '13 at 09:31
  • @aychedee: `split(", ")` won't work if there is no space after comma and it won't split integers separated by whitespace. [@martineau's answer](http://stackoverflow.com/a/14704368/4279) with `replace(",", " ").split()` could be used to support both spaces and commas. Though I'd limit valid input only to spaces for simplicity. – jfs Feb 05 '13 at 09:52
  • Yeah, but he was explicitly replacing spaces with `, `. (comma, space) So :-P – aychedee Feb 05 '13 at 11:52
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.

martineau
  • 119,623
  • 25
  • 170
  • 301
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

Community
  • 1
  • 1
Jendas
  • 3,359
  • 3
  • 27
  • 55
  • 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 :-) – Jendas Feb 05 '13 at 09:26
  • 1
    IMHO, someone new should be shown the simplest way(s) to do it first. – martineau Feb 05 '13 at 09:33
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.

Community
  • 1
  • 1
Volatility
  • 31,232
  • 10
  • 80
  • 89