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
Configuring So That Pip Install Can Work from Github
How to Create Full Compressed Tar File Using Python
How to Dynamically Add/Remove Periodic Tasks to Celery (Celerybeat)
Websocket VS Rest API for Real Time Data
How to Remove the Left Part of a String
Syntaxerror: Multiple Statements Found While Compiling a Single Statement
Multiple Linear Regression in Python
Calculating Arithmetic Mean (One Type of Average) in Python
How to Force Python to Be 32-Bit on Snow Leopard and Other 32-Bit/64-Bit Questions
Super() Raises "Typeerror: Must Be Type, Not Classobj" for New-Style Class
Salt and Hash a Password in Python
Algorithm to Find Which Number in a List Sum Up to a Certain Number
Number of Days Between 2 Dates, Excluding Weekends
What Is This Odd Colon Behavior Doing
Change the Colors Within Certain Range to Another Color Using Opencv
How to Write Inline If Statement for Print
What's the Difference Between Str.Isdigit, Isnumeric and Isdecimal in Python