今天学习了 Python 函数的用法, 了解了使用 Python 如何定义一个函数
而且代码编写过程中也遇到了一些小小的错误, 特此记录一下, 以方便以后在遇到同样错误时能够快速找到问题的点
- # --coding: utf-8 --
- # 定义 4 个简单的函数, 分别是加减乘除, 定义函数要使用 def 这个关键字
- def add(a,b): # 使用 def 关键字定义了 add 这个函数, 给 add 函数指定两个参数 a 和 b
- print "ADDing %d + %d" %(a,b) # 打印出函数中的两个变量
- return a + b #利用 return 语句来返回函数的结果
- # 以下 3 个函数的说明同 add 函数, 就不赘述了
- def subtract(a, b):
- print "SUBTRACTING %d - %d" % (a, b)
- return a - b
- def multiply(a, b):
- print "MULTIPLYING %d * %d" % (a, b)
- return a * b
- def divide(a, b):
- print "DIVIDING %d / %d" % (a, b)
- return a / b
- print "Lets do some math with just functions!"
- age = add(300, 5) #使用 add 函数给 age 变量赋值, 所得的值就是 add 函数中两个变量通过函数 return 后的结果
- height = subtract(78, 4)
- weight = multiply(90, 2)
- iq = divide(4, 2)
- print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
- # A puzzle for the extra credit, type it in anyway.
- print "Here is a puzzle."
下面这个公式稍稍有点复杂, 应该从内往外开始读
解释一下这个公式:
1. 上面分别通过函数计算得到了 ageheightweightiq 的值
2. 然后把这些值套用到函数中得到下面这样一个数学表达式
3. what = 35 + 74 - 180 * 50 / 2
4. 得到的结果就是 - 4391
- what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
- print "That becomes:", what, "Can you do it by hand?"
运行结果如下:
- PS C:\Users\stephen\Desktop\python> python no21return.py
- Lets do some math with just functions!
- ADDing 300 + 5
- SUBTRACTING 78 - 4
- MULTIPLYING 90 * 2
- DIVIDING 4 / 2
- Age: 305, Height: 74, Weight: 180, IQ: 2
- Here is a puzzle.
- DIVIDING 2 / 2
- MULTIPLYING 180 * 1
- SUBTRACTING 74 - 180
- ADDing 305 + -106
- That becomes: 199 Can you do it by hand?
课程中有练习要求使用正常的方法来实现和 what 表达式一样的功能, 不知道我的理解是否正确, 以下是个人的理解: 就是使用简单的数学表达式来完成 what 的赋值
- age = 30+5
- height = 78-4
- weight = 90*2
- iq = 100/2
- print "age is: %d" % age
- print "height is: %d" % height
- print "weight is: %d" % weight
- print "iq is: %d" % iq
- what = age + height - weight * iq / 2
- print "what = age+height-weight*iq/2: %d = %d + %d - %d * %d /2" % (what, age, height, weight, iq )
在代码测试中遇到的问题如下:
语法错误我在定义 add 函数的时候, 在函数尾部掉了冒号
- PS C:\Users\stephen\Desktop\python> python .\No21return.py
- File ".\No21return.py", line 1
- def add(a,b)
- ^
- SyntaxError: invalid syntax
return 的时候参数写错了因为使用的是 Notepad++, 这个软件针对各种语言可以自动弹出语言所自定义的函数以及你在当前脚本中定义过的变量, 优点是可以加快编写代码的速度, 缺点是如果不注意会自动添加不必要的内容, 比如 python, 你输入 "d", 关于 d 的内建函数就会自动给你显示一个列表请注意我代码中的第 7 行, 我在 "return a - b" 的时候无意中把 b 输入成了 basestring, 因此 python 提示整型不能和一个函数类型进行数学运算, 不支持
- PS C:\Users\stephen\Desktop\python> python .\No21return.py
- Lets do some math with just functions!
- ADDing 30 + 5
- SUBTRACTING 78 - 4
- Traceback (most recent call last):
- File ".\No21return.py", line 20, in <module>
- height = subtract(78, 4)
- File ".\No21return.py", line 7, in subtract
- return a - basestring
- TypeError: unsupported operand type(s) for -: int and type
print 的时候漏掉了 "%d"(格式化字符串) 打印的时候给定了变量的值, 但是 print 没有输出
- PS C:\Users\stephen\Desktop\python> python .\No21return-1.py
- age is: 35
- height is: 74
- weight is: 180
- iq is: 50
- Traceback (most recent call last):
- File .\No21return-1.py, line 13, in <module>
- print "what = age+height-weight*iq/2:" % what
- TypeError: not all arguments converted during string formatting
同样的错误, 在代码中少加了一个格式化字符串, 因为格式化输出太多了, 粗心漏掉了
第 13 行代码中, 应有 5 个输出, 但在 print 内容里只写了 4 个
- PS C:\Users\stephen\Desktop\python> python .\No21return-1.py
- age is: 35
- height is: 74
- weight is: 180
- iq is: 50
- Traceback (most recent call last):
- File ".\No21return-1.py", line 13, in <module>
- print "what = age+height-weight*iq/2: %d + %d - %d * %d /2" % (age, height, weight, iq, what)
- TypeError: not all arguments converted during string formatting
小 tips: 在 Linux 中可以使用 cat 命令来查看文档的内容, 在 Windows 的 Powershell 中也有同样的命令:"Get-Content"
Get-Content, 获取指定位置的项的内容
语法: Get-Content [-Path] < 文件路径 >
[-Path] 由方括号引起, 表示可以写, 也可以不写; 不写则默认后面是文件路径, 写了就指名道姓的说后面是文件路径
来源: http://www.bubuko.com/infodetail-2515020.html