示例代码:
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8')
def bmi(name,height,weight):
# 根据身高和体重计算BMI 指数
print(name + "的身高:" + str(height) + "米\t体重:" + str(weight) +"kg")
bmi = weight/(height*height)
print(name + "的BMI指数为:" + str(bmi))
# BMI 指数判断
if bmi < 18.5:
print('营养不良,急需补充营养')
elif bmi >= 18.5 and bmi < 24.9 :
print("正常范围,请注意保持")
elif bmi >= 24.9 and bmi < 29.9:
print('体重过重,请合理膳食')
else:
print('你已经属于肥胖,需要减肥')
bmi('周*刚',1.80,81)
# python3 函数之关键字参数
bmi(height=1.80,weight=81,name='赵四')
# python3 函数之默认参数
def defaultfn(name, st='湖南'):
print(name +'来至于' + st)
defaultfn('王小二','江西')
defaultfn('周小白')
# python3 可变参数之*parameter
def player(*name):
print('\t我喜欢的影视演员有:')
for item in name:
print(item)
player('刘德华','梁家辉','周星驰','姜文','徐峥','吴京')
# python3 可变参数之**parameter
def sign(**sign):
pass
print('\t我喜欢的歌手演员有:')
for key, value in sign.items():
print('[' + key +']来至于:' + value)
sign(刘德华='香港', 李宗盛='台湾')
# pytho3 局部变量定义
def variablepart():
message = "局部变量:message"
print(message)
variablepart()
# pytho3 全局变量定义
allmessage ='全局变量,任意地方都可以被访问到'
def variableglobal():
global globalmessage
globalmessage = '局部变量globalmessage,使用global 关键字进行修饰'
print(globalmessage)
print(allmessage)
variableglobal()
print(globalmessage)
print(allmessage)
# python3 嵌套函数
def counter():
i = 0
def nested():
nonlocal i
i = i + 1
print(i)
return i
print(i)
return nested
num = counter()
print('function counter result is:' + str(num()))
# python3 递归函数调用
def foo(n):
#实现阶乘
if n == 1:
return n # 当满种条件n==1才执行return 操作
res = n*foo(n-1) #调用自已本身的函数的结果(再判断条件是否满足条件)给res ,
return res
print(foo(5))