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." 
1

10 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.

2

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." 
3

When you write x = 0, x is an int...so you can't do x[age1] because x is int

'int' object is not subscriptable is TypeError in Python. To better understand how this error occurs, let us consider the following example:

list1 = [1, 2, 3] print(list1[0][0]) 

If we run the code, you will receive the same TypeError in Python3.

TypeError: 'int' object is not subscriptable 

Here the index of the list is out of range. If the code was modified to:

print(list1[0]) 

The output will be 1(as indexing in Python Lists starts at zero), as now the index of the list is in range.

1 

When the code(given alongside the question) is run, the TypeError occurs and it points to line 4 of the code :

int([x[age1]]) 

The intention may have been to create a list of an integer number(although creating a list for a single number was not at all required). What was required was that to just assign the input(which in turn converted to integer) to a variable.

Hence, it's better to code this way:

name = input("What's your name? ") age = int(input('How old are you? ')) twenty_one = 21 - age if(twenty_one < 0): print('Hi {0}, you are above 21 years' .format(name)) elif(twenty_one == 0): print('Hi {0}, you are 21 years old' .format(name)) else: print('Hi {0}, you will be 21 years in {1} year(s)' .format(name, twenty_one)) 

The output:

What's your name? Steve How old are you? 21 Hi Steve, you are 21 years old 

What are you trying to do here: int([x[age1]])?? It makes no sense.

You just have to cast the age input as an int:

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

You need to convert age1 into int first, so it can do the minus. After that turn the result back to string for display:

name1 = raw_input("What's your name? ") age1 = raw_input ("how old are you? ") twentyone = str(21 - int(age1)) print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years." 
name1 = input("What's your name? ") age1 = int(input ("how old are you? ")) twentyone = str(21 - int(age1)) if age1<21: print ("Hi, " + name1+ " you will be 21 in: " + twentyone + " years.") else: print("You are over the age of 21") 
0

It would be a lot more simple just to do this;

name = input("What's your name? ") age = int(input("How old are you? ")) print ("Hi,{0} you will be 21 in {1} years.".format(name, 21 - age))` 

x is already integer(x=0) and again you trying to make x again integer and also you gave indexing which is beyound the limit because x already has only one indexing (0) and you are trying to give indexing same as age so thats why you get this error. use this simple code

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

Well, all these answers are correct, but here is a more modern way of doing this!

name1 : str = input("What's your name? ") age1 : int = int(input ("how old are you? ")) twentyone : int = 21 - age1 print('Hi, {}, you will be 21 in: {} years'.format(name1, age1)) 

You Might Also Like