multiple_return_values.py

来自「python编程很好的入门材料,包括讲义和源码.」· Python 代码 · 共 28 行

PY
28
字号
#-------------------------------------------------------------------------------
# Define a function which takes a list of numbers and returns its average and 
# how many numbers were in the list.
#-------------------------------------------------------------------------------

def average( numberList ):
    numCount = 0
    runningTotal = 0
    
    for n in numberList:
        numCount = numCount + 1
        runningTotal = runningTotal + n

    # Return the average and the number count
    return runningTotal / numCount, numCount


# Test the average() function...

myNumbers = [5.0, 7.0, 8.0, 2.0]

theAverage, numberCount = average( myNumbers )

print "The average of the list is " + str( theAverage ) + "."
print "The list contained " + str( numberCount ) + " numbers."

raw_input( "\nPress Enter to exit..." )

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?