All right, I’m a novice at python, and here’s the snippet of code in question:
<!-- language: lang-py -->
List = [["W","w"],["A","A"],["a","a"]]
def ascii():
x = 0
y = 0
aValues = [[],[],[]]
for item in List:
for item in List[x]:
c = "0"
c = ord(List[x[y]])
y = y + 1
aValues[x].append(c)
x = x + 1
return aValues
aValues = ascii()
print (aValues)
And, when I try to execute this, I get this error message:
>>>
Traceback (most recent call last):
File "/Users/Hersh/Desktop/Python/ascii_conversion.py", line 16, in <module>
aValues = ascii()
File "/Users/Hersh/Desktop/Python/ascii_conversion.py", line 10, in ascii
c = ord(List[x[y]])
TypeError: 'int' object is not subscriptable
>>>
What exactly is the problem, and how can I fix it?
I’m not sure of what exactly you intend to do with the function, it has several errors. Try this and tell me if this is what you wanted:
EDIT 1 :
Alternatively, if each sublist contains more than two elements, this version is better and will work for the general case:
EDIT 2 :
According to the comments, you need to use a function. All right, then let’s implement the solution as a function – first thing, the input for the function should be received as a parameter, not as a global variable (
List) as you have currently in your code. Then, the result will be returned, and I’ll take the opportunity to show yet another way to solve the problem at hand:Use it like this: