一、If语句密码
1.有数字必须含有大写字母和小写字母
password = 'A12'
A = 'ZXCVBNMASDFGHJKLQWERTYUIOP'
B ='zxcvbnmasdfghjklqwertyuiop'
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('OK')
else:
print('必须含有大写小写和数字')
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))
else :
print('%.2f / %.2f = %.2f'%(num1,num2,num1 / num2))
else:
#抛出错误
raise KeyError('Only choose [+,-,*,/]')
二、for in 循环
range 是前闭后开的
1.银行卡密码
用户只能输入三次密码,如果密码错误则锁定账号
ini_passward = 100000
input_ = int(input('请输入密码>>:'))
for i in range(2):
if input_ == ini_passward:
print('OK')
break
else:
print('密码错误,请尝试重新输入')
input_ = int(input('请输入密码>>:'))
else:
print('账号锁定,请移步至柜台')
2.验证码
生成:纯小写字母验证码4位.
数字字母混合验证码4位,
3次机会
import random
# 随机产生给予范围之内的随机数字.
for i in range(3):
yanzhengma = random.randrange(1000,9999)
print('验证码为: %d' % yanzhengma)
input_ = int(input('请输入验证码>>:'))
if input_ == yanzhengma:
print('欢迎您,您真是一个小天才.')
break
else:
print('验证码错误,请尝试重新输入')
三、