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
- sunny,85,85,false,no
- sunny,80,90,true,no
- overcast,83,86,false,yes
- rainy,70,96,false,yes
- rainy,68,80,false,yes
- rainy,65,70,true,no
- overcast,64,65,true,yes
- sunny,72,95,false,no
- sunny,69,70,false,yes
- rainy,75,80,false,yes
- sunny,75,70,true,yes
- overcast,72,90,true,yes
- overcast,81,75,false,yes
- 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
Post a Comment