I am trying to compare the value of two statutes(open,closed) for 3 times,the values of "OPEN"
s with value of "CLOSED"
s in my list, and count how many times the value of "OPEN"
's are higher than "CLOSED"
's. For its details:
try.txt
OPEN 1
CLOSED 4.2
OPEN 2.755
CLOSED -2.7
CLOSED 27
CLOSED 0.344
CLOSED -0.93
However, I am having an error when random.choice
picks a floating number from the values:
TypeError: object of type 'float' has no len()
I see that random
wants to pick integers as len()
function does not apply to floating numbers. My trial is:
#!/usr/bin/env python
import string
import random
with open('try.txt', 'r') as f:
data = [f.strip('\n').split('\t') for f in open('try.txt')]
print data
for stat,value in data:
#print stat
#print float(value)
randomvalue= random.choice((float(value)))
print randomvalue, "random"
if str(stat)== "OPEN":
print value,"IS THE VALUE OF OPEN"
openvalue = value
#print openvalue
count = 0
n = 0
while n < 3 :
if float(openvalue) > float(randomvalue):
print "OPENISBIGGER"
count = count + 1
n = n + 1
print n, count
If I change the float(random.choice)
to random.choice
only, I receive this output which takes the -
of minus values, and .
of decimals:
1 random
1 IS VALUE OF OPEN
3 0
. random
5 random
2.755 IS VALUE OF OPEN
3 0
- random
2 random
4 random
. random
How do I get pass it?
Aucun commentaire:
Enregistrer un commentaire