Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Why do i get the trouble with “typeerror: 'int' object has no attribute '__getitem__'”?
Next
Answered
Lyric White
  • 5
Lyric White
Asked: May 18, 20222022-05-18T15:47:53+00:00 2022-05-18T15:47:53+00:00In: python

Why do i get the trouble with “typeerror: ‘int’ object has no attribute ‘__getitem__’”?

  • 5

. Advertisement .

..3..

. Advertisement .

..4..

For the problem “typeerror: ‘int’ object has no attribute ‘__getitem__’.” I tried to fix it, but It doesn’t work and returns the result I want. Here is my program:

import math
 import os
 
 
 class collection:
  col = [[0 for col in range(5)] for row in range(6)]
  dist = [[0 for col in range(6)] for row in range(6)]
  filename = ""
  result = ""
 
  def __init__(self,arg1):
  self.filename = arg1
 
  def coll(self):
 
  for i in range(6):
  try:
  if(i==0):
  f = open(self.filename,'r')
  elif(i==1):
  f = open("chap1.txt",'r')
  elif(i==2):
  f = open("chap2.txt",'r')
  elif(i==3):
  f = open("chap3.txt",'r')
  elif(i==4):
  f = open("chap4.txt",'r')
  elif(i==5):
  f = open("chap5.txt",'r')
 
  for j in range(5):
  self.result = f.readline()
  self.col[i][j] = self.result
  finally:
  print "file handling error"
 
  def distance(self):
  for i in range[6]:
  for j in range[6]:
  dis = 0
  for k in range[5]:
  dis += math.fabs((self.col[i][k]-self.col[j][k])*(j-i))
  self.dist[i][j] = dis
  self.dist[i][i] = sys.maxdouble
  return self.dist
 
 class profile:
  dist = [[0 for col in range(6)]for row in range(6)]
  filename = ""
  pque = [[0 for col in range(6)]for row in range(6)]
  d = [[0 for col in range(6)]for row in range(6)]
  par = [[0 for col in range(6)]for row in range(6)]
  st = 0
 
  def __init__(self,arg1):
  self.filename = arg1
 
  def begin(self):
  ob = collection(self.filename)
  ob.coll()
  dist = ob.distance()
 
  def sssp(self):
  for i in range(6):
  pque[i] = sys.maxdouble
  d[i] = sys.maxdouble
  d[0] = 0
  pque[0] = 0
 
  while isempty()==0:
  u = extract_min()
  for i in range(6):
  if d[i]>d[u]+dist[u][i]:
  d[i] = d[u]+dist[u][i]
  pque_deckey(i,d[i])
  par[i]=u
  if u!=0:
  print u
  print "\n"
  for i in range(6):
  print par[i]
 
  def extract_min():
  ret = 0
  shift = 0
  minimum = pque[0]
 
  for i in range(6):
  if pque[i]<minimum:
  minimum = pque[i]
  ret = i
  pque[ret] = sys.maxdouble
  return ret
 
  def isempty(self):
  count = 0
  for i in range(6):
  if pque[i] == sys.maxdouble:
  count=count+1
  if count==6:
  return 1
  else :
  return 0
 
  def pque_deckey(self,im,di):
  pque[im]=di
 
 class main:
  filename = raw_input("enter name of student:\n")
  filename = filename + ".txt"
  if(os.path.exists(filename)==1):
  f = file(filename,"r")
  else:
  f = file(filename,"w+")
  att1 = raw_input("att1 score:\n")
  att2 = raw_input("att2 score:\n")
  att3 = raw_input("att3 score:\n")
  att4 = raw_input("att4 score:\n")
  att5 = raw_input("att5 score:\n")
  f.write(att1)
  f.write("\n")
  f.write(att2)
  f.write("\n")
  f.write(att3)
  f.write("\n")
  f.write(att4)
  f.write("\n")
  f.write(att5)
  f.write("\n")
  stud = profile(filename)
  stud.begin()
  stud.sssp()

and

File "C:\Python27\winculum.py", line 33, in coll
  self.col[i][j] = self.result
 TypeError: 'int' object has no attribute '__getitem__'

has occurred. I’ve checked the entire command line but still can’t find the mistake.

typeerror
  • 2 2 Answers
  • 56 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-09T12:06:48+00:00Added an answer on June 9, 2022 at 12:06 pm

    The cause: This error indicates that you are trying to use the index operator [] on an int rather than a list. So, even when it should be, is col not a list?

    The solution: It seems that the list comprehension overwrites the col variable during iteration, thus use an alternative variable name inside. (Not during the initial iteration when you set col, but throughout subsequent iterations.)

    Take a look at this:

    col = [[0 for col in range(5)] for row in range(6)]
    • 11
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Nathan Vasseur
    2022-05-25T20:26:07+00:00Added an answer on May 25, 2022 at 8:26 pm

    This error could indicate that a variable with the same name was used in your code previously, but for different purposes. It could be that a variable name has been assigned to a function later in the code.

    • 12
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.