return两个返回值_learn python the hard way #ex 21函数的返回值

57dd4c845eedef2c3307c8350504a72b.png

你已经学过使用=给变量命名,将变量定义为某个数字或者字符串。接下来我们将让你见证更多奇迹。我们要给你演示如何使用=以及一个新的Python关键字return来将变量设置为“一个函数的值”。有一点你需要极其注意,不过我们先来编写下面的脚本吧:

code

def add(a, b):
    print(f"ADDING {a} + {b}")
    return a + b
# Python 将两个数字相加,然后当函数结束的时候,它就可以将 a + b 的结果赋予一个变量。

def subtract(a, b):
    print(f"SUBSTRACTING {a} - {b}")
    return a - b

def multiply(a, b):
    print(f"MULTIPLYING {a} * {b}")
    return a * b

def divide(a, b):
    print(f"DIVIDING {a} / {b}")
    return a / b

print("Let's do some math with just function!")

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = multiply(100, 2)

print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")

# A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print("That becomes:", what, "Can you do it by hand?")

result(?)

Let's do some math with just function!
ADDING 30 + 5
SUBSTRACTING 78 - 4
MULTIPLYING 90 * 2
MULTIPLYING 100 * 2
Age: 35, Height: 74, Weight: 180, IQ: 200
Here is a puzzle.
DIVIDING 200 / 2
MULTIPLYING 180 * 100.0
SUBSTRACTING 74 - 18000.0
ADDING 35 + -17926.0
That becomes: -17891.0 Can you do it by hand?

现在我们创建了自己的加减乘除数学函数: add, subtract, multiply, 以及 divide。重要的是函数的最后一行,例如 add 的最后一行是 return a + b,它实现的功能是这样的:

  1. 我们调用函数时使用了两个参数: a 和 b 。
  2. 我们打印出这个函数的功能,这里就是计算加法(adding)
  3. 接下来我们告诉 Python 让它做某个回传的动作:我们将 a + b 的值返回(return)。或者你可以这么说:“我将 a 和 b 加起来,再把结果返回。”
  4. Python 将两个数字相加,然后当函数结束的时候,它就可以将 a + b 的结果赋予一个变量。

和本书里的很多其他东西一样,你要慢慢消化这些内容,一步一步执行下去,追踪一下究竟发生了什么。为了帮助你理解,本节的附加题将让你解决一个迷题,并且让你学到点比较酷的东西。

附加题Q1.如果你不是很确定return的功能,试着自己写几个函数出来,让它们返回一些值。你可以将任何可以放在=右边的东西作为一个函数的返回值。

这里我用了int(input())可以让用户输入自己的值,然后再调用multiply_case函数

(姜酱没有操作)

def add_case(a,b,c):
    print(f"ADDING {a} + {b} + {c}")
    return a + b + c

def multiply_case(a,b,c):
    print(f"MULTIPLYING {a} * {b} * {c}")
    return a * b * c

result1=add_case(10,20,30)

number_of_a=int(input(">>>number_of_a:"))
number_of_b=int(input(">>>number_of_b:"))
number_of_c=int(input(">>>number_of_c:"))
result2=multiply_case(number_of_a,number_of_b,number_of_c)

print(f"result1:{result1},result2:{result2}")

运行结果:

> python .ex21_drills.py
ADDING 10 + 20 + 30
>>>number_of_a:10
>>>number_of_b:20
>>>number_of_c:30
MULTIPLYING 10 * 20 * 30
result1:60,result2:6000

Q2.这个脚本的结尾是一个迷题。我将一个函数的返回值用作了另外一个函数的参数。我将它们链接到了一起,就跟写数学等式一样。这样可能有些难读,不过运行一下你就知道结果了。接下来,你需要试试看能不能用正常的方法实现和这个表达式一样的功能。

变量 what 可以由内到外把嵌套的结构逐步拆分为四个变量x1,x2,x3,x4

def add(a,b):
    print(f"ADDING {a} + {b}")
    return a + b

def subtract(a,b):
    print(f"SUBTRACTING {a} - {b}")
    return a - b

def multiply(a,b):
    print(f"MULTIPLYING {a} * {b}")
    return a * b

def divide(a,b):
    print(f"DIVIDING {a} / {b}")
    return a / b

print("Let's do some math with just functions!")

age=add(30,5)
height=subtract(78,4)
weight=multiply(90,2)
iq=divide(100,2)

print(f"Age:{age},Height:{height},Weight:{weight},IQ:{iq}")

#A puzzle for the extra credit,type it in anyway.
print("Here is a puzzle.")

x1=divide(iq, 2)
x2=multiply(weight, x1)
x3=subtract(height, x2)
x4=add(age, x3)
what=x4

print("That becomes:",what,"Can you do it by hand?")

Q3.一旦你解决了这个迷题,试着修改一下函数里的某些部分,然后看会有什么样的结果。你可以有目的地修改它,让它输出另外一个值。

Q4.颠倒过来做一次。写一个简单的等式,使用一样的函数来计算它。

公式:24+34/100-1023

这里把这个公式转换成使用函数的形式:

def add(a, b):
    print(f"ADDING {a} + {b}")
    return a + b
# Python 将两个数字相加,然后当函数结束的时候,它就可以将 a + b 的结果赋予一个变量。

def subtract(a, b):
    print(f"SUBSTRACTING {a} - {b}")
    return a - b

def multiply(a, b):
    print(f"MULTIPLYING {a} * {b}")
    return a * b

def divide(a, b):
    print(f"DIVIDING {a} / {b}")
    return a / b

print("Let's do some math with just function!")

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = multiply(100, 2)

print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")

# A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")

y1 = iq / 2
y2 = weight * y1
y3 = height - y2
y4 = age + y3
what = y4

print("That becomes:", what, "Can you do it by hand?")

result

E:codevenvScriptspython.exe "E:/code/ex21 加分习题.py"
Let's do some math with just function!
ADDING 30 + 5
SUBSTRACTING 78 - 4
MULTIPLYING 90 * 2
MULTIPLYING 100 * 2
Age: 35, Height: 74, Weight: 180, IQ: 200
Here is a puzzle.
That becomes: -17891.0 Can you do it by hand?

Process finished with exit code 0

这节习题可能会让你有些头大,不过慢慢来,把它当做一个小游戏,解决这样的迷题也是编程的乐趣之一。后面你还会看到类似的小谜题。

常见问题

Q:为什么Python打印公式或函数是反向的?

它们并不是真正的反向的, it's "inside out." When you start breaking down the function into separate formulas and function calls you'll see how it works. Try to understand what I mean by "inside out" rather than "backward."

Q: 怎样使用raw_input() 输入我自己的值?

还记得 int(raw_input())吗? 这样做有一个问题就是你不能输入浮点数,不过你可以使用 float(raw_input())来输入。

Q: 你说的 "写一个公式"是什么意思?

试试先写24 + 34 / 100 - 1023,再用我们的函数转化一下。现在给你的数学公式加入变量,这样它就变成了一个公式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值