Jump to content

Python error: 'type' object is not subscriptable

Go to solution Solved by Slottr,
Just now, _Syn_ said:

h = [float(i) for i in h]

Not sure if they know for loops yet, list comprehensions might be out of range lol

Hello!

I've been working on this program and I don't know why, but there seems to be a problem with it...

txt = "Prob05.in.txt"
msg = open(txt, "r")
msg = msg.read()
h = msg.split("\n")
h=int
g = float
first = True
unSentence = 1
countSentence = 1
while(countSentence <= h[0]):
      while (unSentence <= g[0]):
          Sentence = '\n'
          seq = input (Sentence.format (unSentence))
          print((g[i] - min(lst))/(max(lst) - min(lst))*255)
          unSentence += 1
      countSentence += 1

My file input is:

2
5
0.0
25.0
50.0
75.0
100.0
6
12.3
-67.1
122.8
428.4
-15.9
221.0

 

But when I try to run the program, there is an error:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Collège Sainte Marcelline Prog (8)/Problème 05.py", line 10, in <module>
    while(countSentence <= h[0]):
TypeError: 'type' object is not subscriptable

Anyone has got an idea?

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, boggy77 said:

yes, you're trying to call h[0] (and g[0] for that matter), but h and g are not lists or any other subscriptable objects.

I've only recently started programming with python, so I have no idea what I could do to fix this, but as for the list, it's my input values... I'm really confused :( 

 

Thankyou

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, SamAnw said:

h = msg.split("\n")

h=int

so first you assign msg.split("\n") to h. o it becomes a list as you want it.

but then you assign int to h. so after that line, the value of h is int, instead of being the list you want.

unlike C++, you don't need to initialise variables in python. at least, not in this way.

not sure what you're trying to do with the g variable there.

 


 

 

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, SamAnw said:

h=int

g = float

Not sure what you're trying to achieve with these lines?

 

6 minutes ago, SamAnw said:

I've only recently started programming with python, so I have no idea what I could do to fix this, but as for the list, it's my input values... I'm really confused :( 

 

Thankyou

Iterable objects are things you can parse through one by one. When you go to call h[0] or g[0] python can't do anything, or interpret what you mean because you have g and h set to the classes for an integer or a float.

 

If you're trying to turn those values into an int or float, you need to do it like following:

 

for i in list:
  int(i)
  
 #OR

for i in list:
  float(i)

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, boggy77 said:

so first you assign msg.split("\n") to h. o it becomes a list as you want it.

but then you assign int to h. so after that line, the value of h is int, instead of being the list you want.

unlike C++, you don't need to initialise variables in python. at least, not in this way.

not sure what you're trying to do with the g variable there.

 



 

 

I tried to add the h=int line after the msg.split("\n"), but this type error pops up:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Collège Sainte Marcelline Prog (8)/Problème 05.py", line 10, in <module>
    while(countSentence <= h[0]):
TypeError: 'type' object is not subscriptable

 

As for the g, I'm trying to tell the computer it's a float variable, but I'm not sure it's the right thing to do...

 

Thankyou so much for helping!

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SamAnw said:

I tried to add the h=int line after the msg.split("\n"), but this type error pops up:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Collège Sainte Marcelline Prog (8)/Problème 05.py", line 10, in <module>
    while(countSentence <= h[0]):
TypeError: 'type' object is not subscriptable

 

As for the g, I'm trying to tell the computer it's a float variable, but I'm not sure it's the right thing to do...

 

Thankyou so much for helping!

as I said before, in python you don't need to tell the compute what your variables are. you don't need the lines h=int and g=float.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, boggy77 said:

as I said before, in python you don't need to tell the compute what your variables are. you don't need the lines h=int and g=float.

I took out the lines where I'm trying to tell the computer what the variables are and so this is my code:

txt = "Prob05.in.txt"
msg = open(txt, "r")
msg = msg.read()
h = msg.split("\n")
first = True
unSentence = 1
countSentence = 1
while(countSentence <= h[0]):
      while (unSentence <= g[0]):
          Sentence = '\n'
          seq = input (Sentence.format (unSentence))
          print((g[i] - min(lst))/(max(lst) - min(lst))*255)
          unSentence += 1
      countSentence += 1

But now the type error is:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Collège Sainte Marcelline Prog (8)/Problème 05.py", line 8, in <module>
    while(countSentence <= h[0]):
TypeError: '<=' not supported between instances of 'int' and 'str'

 

That's why I was trying to add the variable=int...

 

Thanks so much 

Sam

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Slottr said:

Not sure what you're trying to achieve with these lines?

 

Iterable objects are things you can parse through one by one. When you go to call h[0] or g[0] python can't do anything, or interpret what you mean because you have g and h set to the classes for an integer or a float.

 

If you're trying to turn those values into an int or float, you need to do it like following:

 


for i in list:
  int(i)
  
 #OR

for i in list:
  float(i)

 

Hello, 

Thanks so much for trying to help, I really appreciate it.

I tried adding for i in list in the program, but I don't think I'm addind it correctly or maybe it just isn't working, but there's still an output error...

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, SamAnw said:

Hello, 

Thanks so much for trying to help, I really appreciate it.

I tried adding for i in list in the program, but I don't think I'm addind it correctly or maybe it just isn't working, but there's still an output error...

What I wrote there was just an example. The list in that for loop would be replaced with whatever list you have in your program (which would be the split and stripped txt file elements).

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, SamAnw said:

I took out the lines where I'm trying to tell the computer what the variables are and so this is my code:


txt = "Prob05.in.txt"
msg = open(txt, "r")
msg = msg.read()
h = msg.split("\n")
first = True
unSentence = 1
countSentence = 1
while(countSentence <= h[0]):
      while (unSentence <= g[0]):
          Sentence = '\n'
          seq = input (Sentence.format (unSentence))
          print((g[i] - min(lst))/(max(lst) - min(lst))*255)
          unSentence += 1
      countSentence += 1

But now the type error is:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Collège Sainte Marcelline Prog (8)/Problème 05.py", line 8, in <module>
    while(countSentence <= h[0]):
TypeError: '<=' not supported between instances of 'int' and 'str'

 

That's why I was trying to add the variable=int...

 

Thanks so much 

Sam

try 

h = float(msg.split("\n"))

 

still not sure what g is

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, SamAnw said:

But now the type error is:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Collège Sainte Marcelline Prog (8)/Problème 05.py", line 8, in <module>
    while(countSentence <= h[0]):
TypeError: '<=' not supported between instances of 'int' and 'str'

The values in the list are actually strings and countSentence is an integer, you need to convert them to floats (not integers because they contain decimals) by doing this

h = msg.split("\n")
h = [float(i) for i in h]

 

There might be a better way of doing this I'm not sure.

Quote or Tag people so they know that you've replied.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, _Syn_ said:

h = [float(i) for i in h]

Not sure if they know for loops yet, list comprehensions might be out of range lol

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

I just wanted to add in though- this is the process you're going to want to follow:

 

- Split and strip each line out of the text file

- This will put everything in a list for you to manage easily with python

- Then you'll want to iterate through that list of elements to convert each one into a specified data type (int or float)

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×