Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: 'int' object is not subscriptable - Python

I was trying a simple piece of code, get someone's name and age and let him/her know when they turn 21... not considering negatives and all that, just random.

I keep getting this 'int' object is not subscriptable error.

name1 = raw_input("What's your name? ") age1 = raw_input ("how old are you? ") x = 0 int([x[age1]]) twentyone = 21 - x print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years." 
like image 723
Gal Appelbaum Avatar asked Nov 22 '11 01:11

Gal Appelbaum


People also ask

How do I fix int object is not Subscriptable in Python?

The TypeError: 'int' object is not subscriptable error occurs if we try to index or slice the integer as if it is a subscriptable object like list, dict, or string objects. The issue can be resolved by removing any indexing or slicing to access the values of the integer object.

What does it mean when an object is not Subscriptable in Python?

The “typeerror: 'int' object is not subscriptable” error is raised when you try to access an integer as if it were a subscriptable object, like a list or a dictionary. To solve this problem, make sure that you do not use slicing or indexing to access values in an integer.

How do you fix method object is not Subscriptable?

Methods are not subscriptable objects and therefore cannot be accessed like a list with square brackets. To solve this error, replace the square brackets with the round brackets after the method's name when you are calling it.

How do you fix int objects not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.


1 Answers

When you type x = 0 that is creating a new int variable (name) and assigning a zero to it.

When you type x[age1] that is trying to access the age1'th entry, as if x were an array.

like image 94
Jonathon Reinhart Avatar answered Sep 24 '22 09:09

Jonathon Reinhart