Infile is a genealogy:
holla 1755 ronaj 1781 asdflæj 1803 axle 1823 einar 1855 baelj 1881 æljlas 1903 jobbi 1923 gurri 1955 kolli 1981 Rounaj 2004 I want to print out every generation time from infile and in the end I want the average. Here I think my issue is that line2 gets out of range when the infile ends:
def main(): infile = open('infile.txt', 'r') line = infile.readline() tmpstr = line.split('\t') age=[] while line !='': line2 = infile.readline() tmpstr2 = line2.split('\t') age.append(int(tmpstr2[1]) - int(tmpstr[1])) print age tmpstr = tmpstr2 infile.close() print sum(age)*1./len(age) main() So I decided to read all information to a list but tmpstr doesn´t change value here:
def main(): infile = open('infile.txt', 'r') line = infile.readline() age=[] while line !='': tmpstr = line.split('\t') age.append(tmpstr[1]) print age infile.close() print sum(age)*1./len(age) main() How come? What's wrong with these two scripts? Why am I writing main() two times? Any ideas how these two can be solved?
Thanx all, this is how it ended up:
def main(): with open('infile.txt', 'r') as input: ages = [] for line in input: data = line.split() age = int(data[1]) ages.append(age) gentime = [] for i in xrange(len(ages)-1): print ages[i+1] - ages[i] gentime.append(ages[i+1] - ages[i]) print 'average gentime is', sum(gentime)*1./len(gentime) main()