Nov-11-2020, 08:58 PM
Hello,
I have been going crazy on a portion of an assignment, I am hoping to get help with. Only using Basic begginer python coding...Functions, loops, files, lists, modules.
In my assignment I have been given a .txt file with a several number on each line ( the numbers represent population amounts). I need to read this file into a list(list 1, I can do this.) Then I have to make 2 more list, one of a list of years between 1950 and 1990(list 2, I can do this) and then a list which stores the population changes from one year to the next(list 3, Need help with this! where to start? For / in?).
I need help on how to make the the list 3, the difference between the populations. How do you compute the difference between sequential elements in a list?
Here is what I have so far... I wanted to make sure I could compute each list needed first before getting into placing these lists into functions and def them ect.
popfile = open("somenumbers.txt", "r") #open File and read into list population
population = popfile.readlines() #list1 from file
popfile.close()
print()
#make list 2 of years 1950-1991
year = list(range(1950,1991))
print(year)
#List 3 Difference in populations
diff_list=[]
for x in range(1,len(population)):
print(population)
diff_list.append(population[x]-population[x-1])
print("the diff_list",diff_list)
With the above I am getting unsupported operand - for str and str:
The file contains 41 lines with each line having a number which represents a population between 1950-1991. (eventually I will use the list of years I make, so someone can enter a year and have the population returned along with other info. When the .txt file is viewed it is in the following format for 41 total lines.(example numbers)
124499
193284
138479
193849
540392
293485
129384
I have been going crazy on a portion of an assignment, I am hoping to get help with. Only using Basic begginer python coding...Functions, loops, files, lists, modules.
In my assignment I have been given a .txt file with a several number on each line ( the numbers represent population amounts). I need to read this file into a list(list 1, I can do this.) Then I have to make 2 more list, one of a list of years between 1950 and 1990(list 2, I can do this) and then a list which stores the population changes from one year to the next(list 3, Need help with this! where to start? For / in?).
I need help on how to make the the list 3, the difference between the populations. How do you compute the difference between sequential elements in a list?
Here is what I have so far... I wanted to make sure I could compute each list needed first before getting into placing these lists into functions and def them ect.
popfile = open("somenumbers.txt", "r") #open File and read into list population
population = popfile.readlines() #list1 from file
popfile.close()
print()
#make list 2 of years 1950-1991
year = list(range(1950,1991))
print(year)
#List 3 Difference in populations
diff_list=[]
for x in range(1,len(population)):
print(population)
diff_list.append(population[x]-population[x-1])
print("the diff_list",diff_list)
With the above I am getting unsupported operand - for str and str:
The file contains 41 lines with each line having a number which represents a population between 1950-1991. (eventually I will use the list of years I make, so someone can enter a year and have the population returned along with other info. When the .txt file is viewed it is in the following format for 41 total lines.(example numbers)
124499
193284
138479
193849
540392
293485
129384