Python Forum
Str int error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Str int error (/thread-26781.html)



Str int error - canatilaa - May-13-2020

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)


RE: Str int error - buran - May-13-2020

reading from file wiyll yield str. convert it to numeric value - e.g. using int() or float()


RE: Str int error - canatilaa - May-13-2020

(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


RE: Str int error - buran - May-13-2020

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]
>>> 



selecting numbers - canatilaa - May-13-2020

thanks for everything