user1580845
user1580845

Reputation: 103

Python: 'int' object is not subscriptable

I am getting an error here and I am wondering if any of you can see where I went wrong. I am pretty much a beginner in python and can not see where I went wrong.

temp = int(temp)^2/key
for i in range(0, len(str(temp))):
    final = final + chr(int(temp[i]))

"temp" is made up of numbers. "key" is also made of numbers. Any help here?

Upvotes: 1

Views: 20413

Answers (2)

Asterisk
Asterisk

Reputation: 3574

final = final + chr(int(temp[i]))

On that line temp is still a number, so use str(temp)[i]

EDIT

>>> temp = 100  #number
>>> str(temp)[0] #convert temp to string and access i-th element
'1'
>>> int(str(temp)[0]) #convert character to int
1
>>> chr(int(str(temp)[0]))
'\x01'
>>> 

Upvotes: 0

Blender
Blender

Reputation: 298166

First, you defined temp as an integer (also, in Python, ^ isn't the "power" symbol. You're probably looking for **):

temp = int(temp)^2/key

But then you treated it as a string:

chr(int(temp[i]))
        ^^^^^^^

Was there another string named temp? Or are you looking to extract the ith digit, which can be done like so:

str(temp)[i]

Upvotes: 5

Related Questions