Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Str int error
#1
f = open("testSEt.txt")
for sayi in f :
 
 can=sayi.split(",") 
 ati=random.sample(can,3)
 x=0
 print((ati[1]))
olasilik=0
if ati[1]>=5:
 olasilik=olasilik+1
 print(olasilik)
    
    
f.close
Quote: File "firstlearning.py", line 14, in <module>
if ati[1]>=5:
TypeError: '>=' not supported between instances of 'str' and 'int'


Hello guys how can I fix the problem.(in the text I have just numbers)
Reply
#2
reading from file wiyll yield str. convert it to numeric value - e.g. using int() or float()
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
(May-13-2020, 06:15 PM)buran Wrote: reading from file wiyll yield str. convert it to numeric value - e.g. using int() or float()
but it'ss list I can't use this command
Reply
#4
you can iterate over list (or use list comprehension) and apply on each element in the list
>>> spam = ['1', '2', '3']
>>> eggs = [int(item) for item in spam]
>>> eggs
[1, 2, 3]
>>> 
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
#5
thanks for everything
Reply


Forum Jump:

User Panel Messages

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