raw_input
returns a string object and ord
returns an integer. Moreover, as the error message states, you cannot add strings and integers together with +
:
>>> 'a' + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>>
However, that is exactly what you are trying to do here:
toConv = [x+key for x in toConv]
In the above code, x
will be an integer (because toConv
is a list of integers) and key
will be a string (because you used raw_input
to get its value).
You can fix the problem by simply converting the input into an integer:
key = int(raw_input("What is the key value or the shift? "))
Afterwards, your list comprehension will work as it should.
Below is a demonstration:
>>> def main():
... inString = raw_input("Please enter the word to be "
... "translated: ")
... # Make the input an integer
... key = int(raw_input("What is the key value or the shift? "))
... toConv = [ord(i) for i in inString]
... toConv = [x+key for x in toConv]
... print "This is toConv", toConv
...
>>> main()
Please enter the word to be translated: word
What is the key value or the shift? 2
This is toConv [121, 113, 116, 102]
>>>