第九章第五节
computing the average
define a function grade_everage() below the grades_sum() function that does the following:
1.has one argument,grades,a list
2.calls grades_sum withgrades
3.computes the average of the grades by dividing that sum by float(len(grades))
4.returns the average
call the newly created grades_average() function with the list of grades and print the result
def grades_sum(n):
total = 0
if x in n:
total += x
return total
print grades_sum(grades)
def grades_average(grades):
return grades_sum(grades)/float(len(grades)
print grades_average(grades)
return grades_sum(grades)/float(len(grades))
想不出来,其实2.3.4三句话的意思用上面的代码,是看了问答版块
第十章第五节
list comprehension syntax
your even_squares list should include the squares of the even numbers between 1 to 11.you list should start[4,16,36…] and go from there.
even_squares = [x ** 2 for x in range(1,11) if x % 2 == 0]
Hint:
you can use x ** 2 to square number,and x % 2 == 0 to check if it's even
x理解错误,看了hint,才知x错误