第21个python程序:函数可以返回东西
2016-09-20 11:33
621 查看
[root@mysql1 pshell]# cat ex21.py
#!/usr/bin/env Python
#-*-coding:utf-8-*-
def add(a,b):
print "adding %d + %d" % (a,b)
return a+b
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(30,5)
height=subtract(78,4)
weight=multiply(90,2)
iq=divide(100,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."
what=add(age,subtract(height,multiply(weight,divide(iq,2))))
print "that becomes: ",what,"can you do it by hand?"
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]# python ex21.py
let's do some math with just functions!
adding 30 + 5
subtracting 78 - 4
multiplying 90 * 2
dividing 100 / 2
age: 35, height: 74, weight: 180, iq: 50
here is a puzzle.
dividing 50 / 2
multiplying 180 * 25
subtracting 74 - 4500
adding 35 + -4426
that becomes: -4391 can you do it by hand?
#!/usr/bin/env Python
#-*-coding:utf-8-*-
def add(a,b):
print "adding %d + %d" % (a,b)
return a+b
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(30,5)
height=subtract(78,4)
weight=multiply(90,2)
iq=divide(100,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."
what=add(age,subtract(height,multiply(weight,divide(iq,2))))
print "that becomes: ",what,"can you do it by hand?"
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]#
[root@mysql1 pshell]# python ex21.py
let's do some math with just functions!
adding 30 + 5
subtracting 78 - 4
multiplying 90 * 2
dividing 100 / 2
age: 35, height: 74, weight: 180, iq: 50
here is a puzzle.
dividing 50 / 2
multiplying 180 * 25
subtracting 74 - 4500
adding 35 + -4426
that becomes: -4391 can you do it by hand?
相关文章推荐
- 笨方法学习Python-习题21: 函数可以返回东西
- python21 函数可以返回一些东西
- 笨办法学python习题21 函数可以返回某些东西
- 笨方法学Python 习题 21: 函数可以返回东西
- 笨方法学习Python 习题21 函数可以返回东西 ---学习记录
- Python 里为什么函数可以返回一个函数内部定义的函数
- Python----__len__可以用len()函数返回对象实例的“长度”
- 习题21 函数可以返回某些东西
- 《笨办法学Python》习题21:函数可以返回东西
- Exercise 21:函数可以返回一些东西
- Learn Python The Hard Way学习(21) - 函数可以返回信息
- 您可以在您的asp程序中使用如下函数来进行在线发信
- 对WinMain程序入口函数返回值为msg.wParam的几点理解
- Wince下eVC开发WinSock程序,connect函数总是立即返回0的问题。
- 用python写的可以简单演示栈的操作的小程序
- 没有返回值的函数中也可以用return
- 阿里巴巴面试算法题:有一个函数int getNum(),每运行一次可以从一个数组V[N]里面取出一个数,N未知,当数取完的时候,函数返回NULL。现在要求写一个函数int get(),这个函数运行一次可以从V[N]里随机取出一个数,而这个数必须是符合1/N
- # include <errno.h >查看错误代码errno是调试程序的一个重要方法。当Linux C API函数发生异常时,一般会将errno变量赋值一个整数,不同的值表示不同的含义,可以通过查看
- python threading获取线程函数返回值
- 对于GetLogicalDrives这个函数,返回一个32位的DWORD类型的值,前26位可以分别表示a-z盘,那后6位表示什么?