Python Forum

Full Version: Str int error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
reading from file wiyll yield str. convert it to numeric value - e.g. using int() or float()
(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
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]
>>> 
thanks for everything