# Roger Guo
# CMSC 210
# Homework Assignment #2
# IHRTLUHC
# Printing a table (modified from the example from the lecture note)
# The data set for this example is farm population in the United States over several decades.
x = [1935, 1940, 1945, 1950, 1955, 1960, 1965, 1970, 1975, 1980]
y = [32.1 ,30.5 ,24.4 ,23 ,19.1 ,15.6 ,12.4 ,9.7 ,8.9 ,7.2]
# Compute averages of the two data sets
count = 0
xSum = 0
for year in x:
count += 1
xSum += year
ySum = 0
for pop in y:
ySum += pop
xBar = xSum / count
yBar = ySum / count
上面这段代码中,年份数组的变量名用
years
更好. 好的变量/函数命名对软件来说是非常重要的, 你要想象一下n年后再看这段代码的时候是不是一下就能看明白.