Error: 'Int' Object Is Not Subscriptable - Python

Error: 'int' object is not subscriptable - Python

The problem is in the line,

int([x[age1]])

What you want is

x = int(age1)

You also need to convert the int to a string for the output...

print "Hi, " + name1+ " you will be 21 in: " + str(twentyone) + " years."

The complete script looks like,

name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
x = 0
x = int(age1)
twentyone = 21 - x
print "Hi, " + name1+ " you will be 21 in: " + str(twentyone) + " years."

TypeError: 'int' object is not subscriptable

If you want to sum the digit of a number, one way to do it is using sum() + a generator expression:

sum(int(i) for i in str(155))

I modified a little your code using sum(), maybe you want to take a look at it:

birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
summ = sum(int(i) for i in birthday[0:2])
sumd = sum(int(i) for i in birthday[3:5])
sumy = sum(int(i) for i in birthday[6:10])
sumall = summ + sumd + sumy
print "The sum of your numbers is", sumall
sumln = sum(int(c) for c in str(sumall)))
print "Your lucky number is", sumln

TypeError: 'int' object is not subscriptable ?smth wrong with pow()?

The problem with your code is that

x = x + pow(int(value[i]), leng)
# Replace above line with
x = x + pow(int(str(value)[i]), leng)

The error is occurring because value is an integer and integers are not subscriptable. Adding the str(value) allows you to access each number by index.

Also, your code is an infinite loop.

This should work according to your description.

def nar( value ):
leng = len(str(value))
x = 0

for i in range(len(str(value))):
x = x + pow(int(str(value)[i]), leng)

# return x
if x == value:
return True
else: return False

print(nar(145))

TypeError: 'int' object is not subscriptable in Python3

"'int' object is not subscriptable" means that you try to use "[]" on an int variable

in you code the L is a list of integers and you access each element in the for loop, then you try to take the int variable (represented as i) and access it like so "i[::-1]" this is a "Typeerror" since "i" is an integer not a list

hope that helps

TypeError: 'int' object is not subscriptable on identical code?

ALWAYS REVERT CHANGES. Caught data = cur.fetchone() which should be fetchall. Which was from me experimenting.



Related Topics



Leave a reply



Submit