All Courses

TypeError: 'int' object is not subscriptable

By Jennifer, 2 years 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

Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Mar 30th (7:00 PM) 516 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More