[Tutor] Please Help

Alan Gauld alan.gauld at btinternet.com
Fri Mar 22 09:56:43 CET 2013


On 22/03/13 07:24, Arijit Ukil wrote:

> f = open ("digi_2.txt", "r+")
> lines = f.readlines()
> for line in lines:
>      number_list = []
>      for number in line.split(','):
>          number_list.append(float(number))
>
> s_data = []
> for i in range(len(number_list)):

You hardly ever need to do this, its much simpler to just iterate over 
the list items directly:

for number in number_list:

>      if number_list[i] > 5:
>          s_data = number_list[i]

Note that you are overwriting your list with a single value.
You need to do what you did above and use append().

> *The problem is: it is printing only the last value, not all the values.
> In this case '10', **not**'9,8,6,10'.*

Because you overwrote the list each time. You need to remember to append 
data to lists not replace the list.



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list