Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Type error!
#1
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?
Reply
#2
what do you think lines 5 and 6 are doing? You assign int to a name h, so the name h now points to the type int and when you try to subscribe it you get the error. You will get the same error later on for g
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Feb-14-2020, 02:13 PM)buran Wrote: what do you think lines 5 and 6 are doing? You assign int to a name h, so the name h now points to the type int and when you try to subscribe it you get the error. You will get the same error later on for g

When I take out the lines 5 and 6, I have another type error:
Error:
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'
Thank you!
Reply
#4
You'll need to convert the text retrieved from the file into numbers for that comparison. In Python, we have the int() and float() functions to do this.

txt = "Prob05.in.txt"
msg = open(txt, "r")
msg = msg.read()
h = msg.split("\n")
g = 
first = True
unSentence = 1
countSentence = 1
while countSentence <= int(h[0]):
      while unSentence <= float(g[0]):
          Sentence = '\n'
          seq = input (Sentence.format (unSentence))
          print((g[i] - min(lst))/(max(lst) - min(lst))*255)
          unSentence += 1
      countSentence += 1
Now, the interpreter did not reach line 11 when you last ran it due to the error on line 10. I'm not sure what g is intended to be so I cannot provide guidance on that.
Reply
#5
When you read from file you get str, you cannot compare (greater than/less than) str and int
you need to convert what you read from file to a numeric type
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,144 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,068 Jul-22-2023, 11:33 PM
Last Post: jhancock
  Python Anytree - Is not of type 'NodeMixin' error georgebijum 3 2,027 May-05-2022, 01:43 PM
Last Post: Gribouillis
  Incorrect Type Error milkycow 4 2,828 Jun-25-2021, 06:04 AM
Last Post: milkycow
Star Type Error: 'in' object is not callable nman52 3 3,328 May-01-2021, 11:03 PM
Last Post: nman52
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,023 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Type Error in Python MarcusB 3 2,527 Mar-30-2021, 06:34 PM
Last Post: buran
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,023 Feb-09-2021, 05:40 PM
Last Post: soft
  Type Error or Value Error? spalisetty06 3 2,339 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,427 Apr-29-2020, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020