您的位置:首页 > 编程语言 > Python开发

Python学习21:Python中函数的用法,使用函数进行简单的数学运算

2018-03-04 01:10 801 查看
今天学习了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 "Let's 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. 上面分别通过函数计算得到了age、height、weight、iq的值
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
Let's 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
Let’s 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]由方括号引起,表示可以写,也可以不写;不写则默认后面是文件路径,写了就指名道姓的说后面是文件路径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python 函数 错误