03 Python函数

一、函数

1.判断商品是否有货,有货则发
def goods(g):
 ku = ['a','b','c','d']
 if g in ku:
 add()
 else :
 return False

def Check_Infor(name,phone,add):
    is_ok = True
    if name == '' or name == ' ':
        is_ok = False
    if len(phone) != 11:
        is_ok = False
    if add not in ['北京','山东']:
        is_ok = False
    return is_ok



def add():
    name = input('请输入姓名:')
    phone = input('请输入电话:')
    add = input('请输入地址:')
    res = Check_Infor(name,phone,add)
    if res:
        note()
    return False


def note():
    #发个信息给商家
    print('马上发货')


def start():
    print('欢迎光临!')
    g = input('商品')
    goods(g)
start()

 

2.注册用户。要求要含有数字和字母、密码要大于6位、电话、验证码(有效期1分钟、无数次重发、睡眠)
global_count = 0
def user():
 users_ = input('请输入账号:>>')
 Z = 'ZXCVBNMASDFGHJKLQWERTYUIOPzxcvbnmasdfghjklqwertyuiop'
 N = '1234567890'
 T = '.*&^%$#@!~'
 is_Z = False
 is_N = False
 is_T = True
 for i in users_:
 # 字母
 if i in Z:
 is_Z = True
 # 数字
 if i in N:
 is_N = True
 # 特殊字符
 if i in T:
 is_T = False
 
 if is_Z and is_N and is_T:
 pass1()
 else:
 print('账号必须含有数字和字母且不能含有(.*&^%$#@!~)')

def pass1():
    password = input('请输入密码:')
    if len(password) > 5:
        phone()
    else:
        print('密码输入失败')


def phone():
    import re,complie('str')
    compile.search
 
    phone = input('请输入电话:')
    if len(phone) != 11:
        print('电话号码无效')
    else:
        verify_number()


def verify_number():
    global global_count    


    import random
    import time
    start_time = time.time()
    res = random.randrange(1000,9999)
    global_count += 1
    print('验证码:{}'.format(res))
    input_ = int(input('请输入验证码:'))
    end_time = time.time()
    sub_time = end_time - start_time
    if sub_time > 10:
        if global_count > 2:
            print('您可能是一个机器人。')
            exit
        print('验证码超时,即将重发')
        time.sleep(2)
        verify_number()
    else:
        if res == input_:
            print('验证码ok,用户注册ok!!!')
        else:
            print('验证码错误!')
            time.sleep(2)
            verify_number()

def start():
    print('新用户注册:')
    user()
start()

 

3.VIP和普通用户抢购
def qiang(a):
    b = '[jocker]'
    if a in b:
        print('恭喜')
    else:
        print('遗憾')
qiang('a')

 

4.不定长参数  (下水道)*+[名字]
def add(*jocker):
    print(jocker)

add(1,2,3)

#
def rock(*jocker):
    sum_ = 0
    if len(jocker) == 0 :
        print('无效数字')
    else:
        for i in jocker:
            sum_ += i
    print(sum_)
rock(1,2,3,4,5,6,7)

 

5.页面之间相互调用
#from [本页面名字] improt [调用的函数]



6.as
#import test as k  k代替test



7.匿名函数
#(lambda x:print(x))(100)
#>>00

#匿名函数  2个数相加
(lambda a,b:print(a+b))(1,2)

猜你喜欢

转载自www.cnblogs.com/wx00/p/11284812.html
今日推荐