• 笨办法21函数的返回值


    代码如下:把第3行的加号换成了减号

     1 def add(a, b):
     2     print "ADDING %d + %d" % (a, b)
     3     return a - b
     4 def substract(a, b):
     5     print "SUBTRACTING %d - %d" % (a, b)
     6     return a - b
     7 def multiply(a, b):
     8     print "MULTIPLYING %d * %d" % (a, b)
     9     return a * b
    10 def divide(a, b):
    11     print "DIVIDING %d / %d" % (a, b)
    12     return a / b
    13 
    14 age = add(30, 5)
    15 height = substract(78, 4)
    16 weight = multiply(90, 2)
    17 iq = divide(100, 2)
    18 
    19 print "Age: %d, Height: %d, Weight: %d, IQ:%d" % (age, height, weight, iq)
    20 
    21 what = add(age, substract(height, multiply(weight, divide(iq, 2))))
    22 print "That becomes:", what, "Can you do it by hand?"

    运行结果可见,return的值和定义变量里print那一行的内容无关

    这里写图片描述


    总觉得冗余的内容太多反而不利于理解,以下简化代码:

     1 def add(a, b):
     2     return a - b
     3 def substract(a, b):
     4     return a - b
     5 def multiply(a, b):
     6     return a * b
     7 def divide(a, b):
     8     return a / b
     9 
    10 age = add(30, 5)
    11 height = substract(78, 4)
    12 weight = multiply(90, 2)
    13 iq = divide(100, 2)
    14 
    15 print "Age: %d, Height: %d, Weight: %d, IQ:%d" % (age, height, weight, iq)
    16 
    17 what = add(age, substract(height, multiply(weight, divide(iq, 2))))
    18 print "That becomes:", what, "Can you do it by hand?"

    输出结果: 
    这里写图片描述

  • 相关阅读:
    力学,结构动力NewMark法软组织模拟
    力学,非线性形变
    力学,线性形变
    波动方程水面模拟(简版)
    FFT海面(简版)
    测地极坐标参数化纹理贴图
    参数化离散指数映射纹理贴图
    Gridview中各个事件的操作以及FindControl
    CSS样式
    博客声明(博客大多均非原创文章,只用于记录)
  • 原文地址:https://www.cnblogs.com/p36606jp/p/7648275.html
Copyright © 2020-2023  润新知