list - python if-else statement returning true only for if condition -


i using python read numeric weather data file , checking humidity conditions. if humidity less or equal 75 humidity should re-written "low" , if humidity greater 75 should re-written "high". following data in file.

outlook, temperature, humidity, windy, permission_to_play

  1. sunny,85,85,false,no
  2. sunny,80,90,true,no
  3. overcast,83,86,false,yes
  4. rainy,70,96,false,yes
  5. rainy,68,80,false,yes
  6. rainy,65,70,true,no
  7. overcast,64,65,true,yes
  8. sunny,72,95,false,no
  9. sunny,69,70,false,yes
  10. rainy,75,80,false,yes
  11. sunny,75,70,true,yes
  12. overcast,72,90,true,yes
  13. overcast,81,75,false,yes
  14. rainy,71,91,true,no

i reading file in list , accessing humidity value. following code have written.

def fetchdata(filename):     datalist = []     rd =open(filename,mode='r')     list = rd.readlines()     l in list:         sublist = l.strip().split(',')         humidity=sublist[2]         if humidity>75:             sublist[2]="high"         else:             sublist[2]="low"         datalist.append(sublist)     return datalist  datalist = fetchdata('weather.numeric.data') print datalist 

after execution of this, data numbers 6,7,9,11,13 should have humidity value low , others should high. humidity values becoming high, seen in output below.

[['sunny', '85', 'high', 'false', 'no'], ['sunny', '80', 'high', 'true', 'no'], ['overcast', '83', 'high', 'false', 'yes'], ['rainy', '70', 'high', 'false', 'yes'], ['rainy', '68', 'high', 'false', 'yes'], ['rainy', '65', 'high', 'true', 'no'], ['overcast', '64', 'high', 'true', 'yes'], ['sunny', '72', 'high', 'false', 'no'], ['sunny', '69', 'high', 'false', 'yes'], ['rainy', '75', 'high', 'false', 'yes'], ['sunny', '75', 'high', 'true', 'yes'], ['overcast', '72', 'high', 'true', 'yes'], ['overcast', '81', 'high', 'false', 'yes'], ['rainy', '71', 'high', 'true', 'no']] 

what changes should make? in advance! :)

you should convert string int before comparing 75:

if int(humidity)>75: 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -