zoukankan      html  css  js  c++  java
  • 笨办法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?"

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

  • 相关阅读:
    poj1275收银员——差分约束
    poj3565Ants——KM算法
    bzoj2750Road——最短路计数
    poj1236学校网络——连通块
    poj2226Muddy Fields——二分图匹配
    Pots
    蜘蛛牌
    Find The Multiple (水题)
    Dungeon Master (三维bfs)
    棋盘问题 (简单搜索)
  • 原文地址:https://www.cnblogs.com/p36606jp/p/7648275.html
Copyright © 2011-2022 走看看