Bottom Page

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?
Quote
#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
Quote
#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!
buran wrote Feb-14-2020, 03:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#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.
SamAnw likes this post
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Type hinting - return type based on parameter micseydel 2 122 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Type Error: bad operand type for unary +: 'str' Psypher1 2 2,337 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  catch input type error mcmxl22 5 364 Aug-11-2019, 07:33 AM
Last Post: wavic
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 2,501 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  Type error when reading in different data types on an __init__ method Dylanmull 3 419 May-09-2019, 02:05 PM
Last Post: buran
  Getting error "Type error-a bytes-like object..." mrapple2020 1 2,079 Apr-06-2019, 06:37 PM
Last Post: mrapple2020
  scatter plot error - possibly data type - TypeError: nan is not a string chudson 1 1,794 Mar-24-2019, 11:48 AM
Last Post: chudson
  Unexpected expected type error result MartinMaker 1 439 Feb-16-2019, 05:02 PM
Last Post: micseydel
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 381 Feb-07-2019, 03:15 PM
Last Post: coltron1282
  Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo kneesarethebees 1 1,205 Aug-02-2018, 01:39 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)