Day_03-函数和模块的使用

使用函数求阶乘

使用while循环的代码:

m = float(input('m = '))
n = float(input('n = '))
mn = m - n
fm = 1
while m != 1:
  fm *= m
  m -= 1
fn = 1
while n != 1:
  fn *= n
  n -= 1
fmn = 1
while mn != 1:
  fmn *= mn
  mn -= 1
print(fm // fn // fmn)

定义函数块:

def C_N_M(parm):
  fmn = 1
  for num in range(1, parm):
  fmn *= num
  return fmn

函数的使用

函数是用来重复使用的,定义函数是有套路的,首先,要会写出裸代码,找出重复使用的部分,接下来将需要重复使用的代码转换成参数,带入到函数中。

def funCname([param]):
  执行体
  [return]
例子:
def Joker():
  print('hello')
Joker()
函数可以当作判断条件
def Joker():
  print('hello')
Joker()
if Joker:
  print('hahaha')
带参函数:
def zqq(inpute):
  print('%s 你真帅'%inpute)
zqq('李现,王嘉尔')
 
# 商品秒杀 优质用户(vip)才可以秒杀到商品
import time
def vip_(vip):
  a = ['user-123','zqq','aaa'] # 会员库
  if vip in a:
  time.sleep(1)
  print('秒杀成功')
  else:
  print('秒杀失败')
 
python中函数里是有返回值的,当没有返回值是返回的是none,否则返回值
 
# pwd = input('Password:')
# A = '1234567890'
# B = 'QAZWSXEDCRFVTGBYHNUJMIKOLP'
# C = 'qazwsxedcrfvtgbyhnujmikolp'
# c1,c2,c3=False,False,False
# for i in pwd:
# if i in A:
# c1 = True
# if i in B:
# c2 = True
# if i in C:
# c3 = True
# if c1 and c2 and c3 and len(pwd) >= 6:
# print('ok')
# else:
# print('error')
 

猜你喜欢

转载自www.cnblogs.com/KAJIA1/p/11280395.html
今日推荐