• Register
1 vote
24 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,240 points

Related questions

0 votes
1 answer 23 views
23 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 8 views
8 views
Problem: I am following instructions for a class homework assignment and I am supposed to look up the top 200 most frequently used words in a text file. Here's the last part of the code: fdist1 = FreqDist(NSmyText) vocab=fdist1.keys() vocab[:200] But when I ... 1, in <module> TypeError: 'dict_keys' object is not subscriptable Any suggestions on how to fix this so it can correctly return an answer?
asked 3 days ago Mashhoodch 9.1k points
0 votes
1 answer 49 views
49 views
Problem: when I make a function call to this method create({'1','2'}) I get an TypeError: 'set' object is not subscriptable error on line 'AWS': list(map(lambda id: f"arn:aws:iam::{id}:root", ids[i:i + 200])). Coming from a java background, is this somehow related to typecasting? Does the error mean that I'm passing a set data structure to a list function? How could can this be resovled?
asked 5 days ago Mashhoodch 9.1k points
0 votes
1 answer 6 views
6 views
Problem: typeerror: '_io.textiowrapper' object is not subscriptable.
asked 5 days ago Dan phillip 1.9k points
0 votes
1 answer 17 views
17 views
Problem: I'm making a small program for math (no particular reason, just kind of wanted to) and I ran into the error "TypeError: 'NoneType' object is not subscriptable. I have never before seen this error, so I have no idea what it means.
asked Feb 4 Mashhoodch 9.1k points
0 votes
1 answer 5 views
5 views
Problem: I've tried to solve an assignment with this code: bank_holiday= [1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 0, 2] #gives the list of bank holidays in each month def bank_holiday(month): month -= 1#Takes away the numbers from the months, as months start at 1 ( ... "))) But when I run it, I get the error: TypeError: 'function' object is not subscriptable I don't understand where this is coming from...
asked Jan 24 Mashhoodch 9.1k points
0 votes
1 answer 18 views
18 views
I got stuck with this error. can anyone help thanks in advance
asked Jan 22 Lalit Kumar 1.2k points
0 votes
1 answer 37 views
37 views
Problem: I am not an experienced programmer. I am trying to run following program: value = True number = [1,2,3,4] for i in number: if i%2 == 0 : value = True else: value = False for i in range(0, len(number)): print("The sequece of values is: ", ... false. The number that completely divides by 2 should print true and the others should print false. I have no idea what I am doing wrong. Any help?
asked Jan 16 Code Learner 5.7k points
0 votes
1 answer 24 views
24 views
Problem: Hi there! I am a beginner and do not have much experience in programming. I am trying to run the following program: entries = int(input("Enter the number of items you want to add in the list: ")) lis = [] for i in range(1, entries+1): lis.append ... not know what I am doing wrong. This program was working correctly till yesterday, but I do not know what is wrong with it now. Please help.
asked Jan 16 Code Learner 5.7k points
0 votes
1 answer 11 views
11 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, 2020 psandprop 2.4k points