TypeError: 'int' object is not subscriptable

By Jennifer, a year ago
  • Bookmark
0

name = input("What's your name? ")
age = input ("how old are you? ")
n = 0
int([n[age]])
twentyone = 21 - n
print ("Hi, " + name + " you will be 21 in: " + twentyone + " years.")
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable

How to solve this error?

Python
Typeerror
1 Answer
1
Pritam Shinde

The problem is in the line,

int([n[age]])

What you want is

n = int(age)

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

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

The complete script looks like,

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


Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More