Lección tortuga pequeña 14: La introspección Resumen de cadena

2. fichero1 = abierto ( 'C: \ windows \ temp \ readme.txt', 'r') representa el abierto de sólo lectura "C: \ windows \ temp \ readme.txt" este archivo de texto, pero de hecho esta declaración se quejan, ¿sabes por qué? ¿Cómo se modifique? **
A:

Se quejará porque en la cadena, estuvimos de acuerdo "\ t" y "\ r" representa "pestaña horizontal (TAB)" y "retorno de carro" y por lo tanto no sigue el camino de nuestro plan para abrir el archivo . Python abrió el camino para que resolvamos, sólo tiene que utilizar el operador de cadena en bruto (R o R) a:

fichero1 = abierto (R'C: \ windows \ temp \ readme.txt', 'r')

Autor: villanos Inocencia
enlace: https: //www.jianshu.com/p/ae903027f184
Fuente: libros de Jane
tienen derechos de autor por el autor. reimpresión comercial póngase en contacto con el autor autorizada, reimpresión no comercial por favor indique la fuente.

6. Se dice que sólo el CI superior a 150 aceite de pescado puede resolver la cadena (reducido a una cadena que tenga sentido): str1 = 'i2sl54ovvvb4e3bferi32s56h; $
c43.sfc67o0cm99' Respuesta: str1 [:: 3]
### Tenga en cuenta que el paso longitud de tres se cuenta desde la primera letra, espaciado de dos personajes!

El problema de programación
0. Por favor, escriba un código de los controles de seguridad de contraseña guión: check.py

需求:
   低级密码要求:
#   1. 密码由单纯的数字或字母组成
#   2. 密码长度小于等于8位

   中级密码要求:
#   1. 密码必须由数字、字母或特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)任意两种组合
#   2. 密码长度不能低于8位

   高级密码要求:
#   1. 密码必须由数字、字母及特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)三种组合
#   2. 密码只能由字母开头
#   3. 密码长度不能低于16位
首先放上自己的错误代码:并对其进行分析
str ='~!@#$%^&*()_=-/,.?<>;:[]{}\|'
num = 0
eng = 0
str1= 0
both = 0
while True:
#这里注意whlie Ttue 是大写T
    temp = input("请设置一个密码:")
    length = len (temp)
    while temp.isspace() or length == 0:
        print('输入不能为空,请重新输入:')
        temp = input()
        length = len (temp)
        #由于这个定义在while循环外,所以需要重新定义
    for each in temp:
        if each.isdigit():
            num = 1
        if each.isalpha():
            eng = 1
        if str.find(each):
            str1= 1
            #这里写的有点问题,正确的应该直接用检查符号in
          修改为  if each in str:
                     str1= 1
            #
        both = (num) + (eng) + (str1)
    if length <= 8 and temp.isalnum():
        print('您输入的密码安全等级为初级,不符合要求!')
    elif 8<length<16 and both == 2:
        print('您输入的密码安全等级为中级,不符合要求!')
        ##这里虽然和题目符合,但是纵观所有情况,若用户输入为16位以内且有三种情况时,会出bug,所以后面将both换成 >= 2
    elif length >= 16 and both == 3:
        print('您输入的密码安全等级为高级,可以注册!')
       **#这里漏了一个条件,开头字符不能为特殊字符,因此需要加入temp(0).isalnum,来确保第一位!!**
    if both <= 2:
        print("""请以一下方式改善您的密码:
            1.数字,字母或者特殊字符组合
            2.密码长度尽量在16位以上""")
        break
        #这里写的有问题,应该是继续循环操作,或者用户需要自主退出,因此需要把开头的whlie循环改为一个while条件,从而进行退出
    
改进后:
str ='~!@#$%^&*()_=-/,.?<>;:[]{}\|'
t = 'y'
while t == 'y':
    num = 0
    eng = 0
    str1= 0
    both = 0
    level = 0
    #这里改进进while里面,每次循环重新定义,不然可能会代入以前的值
    # 加入level的原因是,在中级且输入为三种符号时,不会print 后面的帮助条件(详情见错误中倒数第五行),因此加入level用来定义当为初中级为0,高级为1.
    temp = input("请设置一个密码:")
    length = len (temp)
    while temp.isspace() or length == 0:
        print('输入不能为空,请重新输入:')
        temp = input()
        length = len (temp)
    for each in temp:
        if each.isdigit():
            num = 1
        if each.isalpha():
            eng = 1
        if each in str:
            str1= 1
        both = (num) + (eng) + (str1)
    if length <= 8 and temp.isalnum():
        print('您输入的密码安全等级为初级,不符合要求!')
    elif 8<length<16 and both >= 2 and temp[0].isalnum():
    #temp[0].isalnum()防止开头为特殊字符!!!!!!注意!字符串中位置是用中括号[]
        print('您输入的密码安全等级为中级,不符合要求!')
    elif length >= 16 and both == 3 and temp[0].isalnum():
         print('您输入的密码安全等级为高级,符合要求!')
         level = 1
    #提示栏
    if level != 1:
        print("""请以一下方式改善您的密码:
            1.数字,字母或者特殊字符组合
            2.密码长度尽量在16位以上
            3.开头不能为特殊字符""")
     #选择是否退出栏   
        print('继续请按y,否则任意键退出',end='')
        t = input()

Resumen: Esta lección de programación que realmente me dije pensando un programa, no siguen un pensamiento fijo al sujeto, pero debe definir un conjunto de caracteres, por lo que la situación general en un caso separado, ya que ambos tienen carácter sino también digitales , la cadena no dichos caracteres por lo tanto sólo definidos funciones incorporadas y función,, números y comprueba entonces utilizan literalmente en el caso en el que la salida es 0 o 1, a continuación, se integra (sumado) como una situación general en donde la determinación (lógica en un sentido digital)

PS: una versión pequeña tortuga de la respuesta: De hecho, la misma lógica se utiliza para convertir el pensamiento digitales

symbols = "~!@#$%^&*()_=-/,.?<>;:[]{}\|"
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
t = 'y'
while t == 'y':
    passwd = input('您输入的密码为空(或空格),请重新输入:')
    length = len(passwd)    
#判断长度
    while(passwd.isspace() or length == 0):
        passwd = input('您输入的密码为空(或空格),请重新输入:')
        length = len(passwd)
    if length <= 8:
        flag_len = 1
    elif 8 < length <16:
        flag_len = 2
    else:
        flag_len = 3
    flag_con = 0
#判断是否包含特殊字符
    for each in passwd:
        if each in symbols:
            flag_con +=1
            break
#判断是否包含字母
    for each in passwd:
        if each in chars:
            flag_con += 1
            break
#判断是否包含数字
    for each in passwd:
        if each in nums:
            flag_con += 1
            break
#打印结果
    while 1:
        print("您的密码安全级别评定为:",end='')
        if flag_len == 1 or flag_con == 1:
            print("低")
        elif flag_len == 2 or flag_con == 2:
            print("中")
        else:
            print("高")
            print("请继续保持")
            break
        print("""请按以下方式提升您的密码安全级别:
    1.密码必须由数字、字母及特殊字符三种组合
    2.密码只能由字母开头
    3.密码长度不能低于16位""")
        break
    t = input("还要再测试么?(”y“继续,其他退出)")
Publicado 17 artículos originales · ganado elogios 1 · visitas 359

Supongo que te gusta

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105249965
Recomendado
Clasificación