首页 > 技术文章 > day0203

wqs-Time 2019-08-01 20:57 原文

 

day02

1.for i in range() --->用于设置for循环的迭代设置。
ranage 也是一个前闭后开的。

2.random.randrange() --->随机产生给予范围之内的随机数字
random.randrange(1000,9999)

day03


1.python的缺点 精度丢失

 

1.例题

#密码包含大小写和数字
username = input('请输入用户名:')
password = input('请输入密码:')
A ='qwertyuiopasdfghjklzxcvbnm'
B ='QWERTYUIOPASDFGHJKLZXCVBNM'
C ='1234567890'
count1,count2,count3 = False,False,False
for i in password:
  if i in A :
  count1 = True
  if i in B :
  count2 = True
  if i in C :
  count3 = True
if count1 and count2 and count3 :
  print('注册成功')
else :
  print('密码必须含有大写,小写和数字')

2.例2

#输入两个数字进行加减乘除
num1,num2 = map(float,input('请输入Num1和Num2:').split(','))
choose_method = input('Choose Method:[+,-,*,/]')
  if choose_method in '+-*/':
    if choose_method == '+':
      print('%.2f + %.2f = %.2f'%(num1,num2,num1+num2))
    elif choose_method == '-':
      print('%.2f - %.2f = %.2f'%(num1,num2,num1-num2))
    elif choose_method == '*':
      print('%.2f * %.2f = %.2f'%(num1,num2,num1*num2))
  elif choose_method == '/':
print('%.2f / %.2f = %.2f'%(num1,num2,num1/num2))
 
  else:
    #抛出错误
    raise KeyError('Only choose [+,-,*,/]')
 

 

3.例3

#三次输入错误密码锁死
passs = 123456
  for i in range(3):
    password = int(input('请输入密码:'))
    if password == passs :
      print('登录成功')
      break
    else:
      print('请重试')
  else:
    print('您已三次输入密码错误,锁死')

 

4.例4

#随机生成四位数字验证码
import random
yanzhengma = random.randrange(1000,9999)
print(yanzhengma)
 
5.例5
#简易购物车
def Check_Goods(g):
  G = ['汽车','火车','飞机']
  if g in G:
    Address()
  else:
    return False
def Check_Information(name,phone,addr):
  is_Ok = True
  if name == "" or name == "":
    is_Ok = False
  if len(phone) != 11 :
    is_Ok = False
  if addr not in ['北京','山东']:
    is_Ok = False
  return is_Ok

def Address():
  name = input('请输入姓名:')
  phone = input('请输入电话:')
  addr = input('请输入地址:')
  res = Check_Information(name,phone,addr)
  if res:
    Note()
  return False
def Note():
  print('马上发货')
def Start():
  print('欢迎光临')
  g = input('商品:')
  Check_Goods(g)
Start()

 

6.例6

#注册用户

def Users():
  users_ = input('请输入用户名:')
  Z = '123456789'
  N = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
  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:
    password()
   else:
    print('密码必须由数字和字母组成,且不能有特殊字符')

def password():
  passwd = input('请输入密码:')
  if len(passwd) >= 6:
    phone()
  else:
    print('密码必须大于6位,请重试')
def phone():
  import re
  compile = re.compile('str')
  compile.search
  compile.findall
 
  pho = input('请输入手机号:')
  if len(pho) == 11:
    verify()
  else:
    print('手机号输入错误,请重试')
  global_count = 0
def verify():
#声明变量是全局的
  global global_count
  import random
  import time
  yanzhengma = random.randrange(1000,9999)
  global_count +=1
  start_time = time.time()
  print('向您手机发送的验证码为: %d'%yanzhengma)
  veri = int(input('请输入验证码:'))
 
  end_time = time.time()
  sub_time = end_time - start_time
 
  if sub_time > 2:
  if global_count>2:
    print('请稍后重试。')
    exit()
  print('验证码超时,即将重新发送')
  time.sleep(2)
  verify()

  else:
    if yanzhengma == veri:
      print('注册成功')
    else:
      print('验证码错误,请重试')
      time.sleep(2)
      verify()
def Start():
  print('请注册:')
  Users()
 
Start()

 

 

推荐阅读