• Register
1 vote


I was trying to write code, in which I’ll get someone's name and age but I keep getting this error: 'int' object is not subscriptable error.

Here is my code

name = raw_input("What's your name? ")
age = raw_input ("how old are you? ")
x = 0
twentyone = 21 - x
print "Hello, " + name+ " you will be 21 in: " + twentyone + " years."

Any clues?

12 7 7
15,250 points

1 Answer

1 vote


The problem occurred when you wrote x=0 and then int([x[age]]). When you declared x=0, it created an int variable which value is 0. Then again when you wrote int([x[age]]), compiler assumed it as it an array but you didn’t declare it as an array.
The whole program would be like this

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

It should work fine now.

13 9 6
94,240 points

Related questions

0 votes
1 answer 22 views
Can I represent a string as number in python or how can I check if there is any number in string of python function. Supose a function is: def My_num(x): try: float(x) return True except ValueError: return False my question is calling float in the main function is ok or not and I am not getting my expected result.
asked Mar 11 LizzyM 6.1k points
0 votes
1 answer 5 views
from newsapi.sources import Sources import json api_key ='*******************' s = Sources(API_KEY=api_key) they input the category of news they want wanted = input('> ') source_list = s.get(category=wanted, language='en') index = 0 sources = [] getting ... 1 getting error 'type' object is not subscriptable on the line a = Article[url_] have researched but still do not understand why in my case.
asked Oct 25 psandprop 2.4k points
0 votes
1 answer 12 views
On making a small program for math there returned an error "TypeError: 'NoneType' object is not subscriptable. Never before has been seen, this error, so no idea what it means. import math print("The format you should consider:") print str("value 1a")+str(" + ")+ ... /Nathan/Documents/Python/New thing", line 16, in <module> a = lista[1] - lista[0] TypeError: 'NoneType' object is not subscriptable
asked Oct 24 psandprop 2.4k points
1 vote
1 answer 215 views
Problem: Hello good souls, I am having an error running a simple piece of python codes. Find my codes snippet below: def sumList(l): if l == []: return 0 else: return sumList[1:] + [l[0]] def main(): l=[3,2,5,3] print(sumList(l)) main() From the ... , I couldn&rsquo;t find anything in my favor. Would you please have a look at the snippet and help me to fix the issue? Thanks for saving my day.
asked Jul 11 adamSw 11.3k points
1 vote
2 answers 924 views
Problem: Hello all, I am encountering an error with my python script. I am trying to play with map object in my script. Instead of returning a true result, it is throwing an error: typeerror: 'map' object is not subscriptable. What is this error? Literally, ... the error. I am looking forward to having a legit solution to this problem. I must appreciate your concern and valuable help. Thanks all.
asked Apr 25 Gavin 15.3k points