• Register
1 vote
29 views

Problem:

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
int([x[age]])
twentyone = 21 - x
print "Hello, " + name+ " you will be 21 in: " + twentyone + " years."

Any clues?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

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,260 points

Related questions

0 votes
1 answer 26 views
26 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, 2020 LizzyM 6.1k points
0 votes
1 answer 9 views
9 views
Problem: 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. Error: 'int' object is not subscriptable
asked Mar 31 ummesalma 29.2k points
0 votes
1 answer 18 views
18 views
Problem: how to I Solve this: int object is not subscriptable python 3 HELP! I need help understanding this: int object is not subscriptable python 3 Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 8 Mashhoodch 13k points
0 votes
1 answer 10 views
10 views
Problem: I opened my computer and i suffered from this problem, then i think to post question to here, if anyone there who know about this then please answer me.
asked Apr 3 niyatiry 3.9k points
1 vote
1 answer 4 views
4 views
Problem: Can some one help me in correction of » Python typeerror: 'nonetype' object is not subscriptable.
asked Mar 30 PkGuy 23.5k points
0 votes
1 answer 9 views
9 views
Problem: error. Am I doing something wrong or am I just looking at it the wrong way? TypeError: 'float' object is not subscriptable
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 29 views
29 views
Problem: Why am I getting this error, from line 5 of my code, when attempting to solve Project Euler Problem 11? 'generator' object is not subscriptable
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 31 views
31 views
Problem: I'm writing some code to create an unsorted list but whenever I try to insert a list using the insert method I get the 'method' object is not subscriptable error. Not sure how to fix it. Thanks.
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 34 views
34 views
Problem: have multiple files that I need to load so I'm using a dict to shorten things. When I run I get a TypeError: 'type' object is not subscriptable
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 10 views
10 views
Problem: I am getting this error please help: Typeerror: 'zip' object is not subscriptable
asked Mar 16 ummesalma 29.2k points