Small turtle Lesson 14: Introspection string summary

2. file1 = open ( 'C: \ windows \ temp \ readme.txt', 'r') represents the open read-only "C: \ windows \ temp \ readme.txt" this text file, but in fact this statement will complain, you know why? How would you modify? **
A:

Will complain because in the string, we agreed "\ t" and "\ r" represent "horizontal tab (TAB)" and "carriage return" and therefore does not follow the path of our plan to open file . Python paved the way for us to solve, just use the raw string operator (R or r) to:

file1 = open(r’C:\windows\temp\readme.txt’, ‘r’)

Author: Innocence villains
link: https: //www.jianshu.com/p/ae903027f184
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

6. It is said that only IQ higher than 150 fish oil can solve the string (reduced to a meaningful string): str1 = 'i2sl54ovvvb4e3bferi32s56h; $
c43.sfc67o0cm99' Answer: str1 [:: 3]
### Note that step length of three is counted from the first letter, spacing two characters!

The programming problem
0. Please write a password security checks script code: 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()

Summary: This lesson of programming that actually told myself thinking a program, do not follow a fixed thought to the subject, but should define a set of characters, so that the overall situation into a separate case, as both have character but also digital , the string no such built-in functions and function, thus only defined characters, numbers, and then checks literally use for in the case where the output is 0 or 1, then it is integrated (summed) as an overall situation where the determination (logic into a digital sense)

ps: a small turtle version of the answer: In fact, the same logic is used to convert digital thinking

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“继续,其他退出)")
Published 17 original articles · won praise 1 · views 359

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105249965