# -*- coding: utf-8 -*- """ Created on Fri Sep 23 11:00:26 2022 @author: cpresser """ #tables of values # name1 data1,1 data1,2 data1,3 # name2 data2,1 ... # ... #e.g Country population by year #read names from the user DATA_POINTS = 4 #number of values of data in each "row" names = [] averages = [] #store the data: data = [] #each item will be a list of the 4 values #initialization name = input("Enter a name (q to quit): ") while name != "q": #repeat (for each name the user enters) #add it to the list names.append(name) # a list of values associated with this name values = [] sum = 0 #get the data that goes with it for i in range(DATA_POINTS): value = float(input("Enter a number: ")) sum = sum + value values.append(value) #add the values to the data list data.append(values) #compute the average avg = sum/DATA_POINTS #store the avarage averages.append(avg) #next name = input("Enter a name (q to quit): ") #these are parallel lists #print(names) #print(averages) #print items together #use a for loop through the indices # 0..length of names list for i in range(len(names)): #print(f"{names[i]:10}{averages[i]:10}{data[i]}") print(f"{names[i]:10}{averages[i]:10}", end=": ") for j in range(len(data[i])): print(data[i][j], end=" ") print() #NOTE: data[i] is a list of four values # zip together the parallel list #print( zip(names, averages) ) for n, a in zip(names, averages): print(f"{n:10}{a:>10.5f}") # the list of lists print(data) #print one of the lists print(data[1]) #print an item in the list print(data[1][3])