首页 > 解决方案 > 为什么脚本没有检测到“星”这个词?

问题描述

我编写了一个 Python 脚本来检查密码,但这个脚本无法检测到 word "star"

我的脚本如下:

import os
import sys
import time

while True:
    user_password = input("Enter your password: ")

    # Processing String.
    try:
        user_password = int(str(user_password))
        inta = True
    except Exception:
        user_password = str(user_password).lower()
        inta = False
    passlen = len(str(user_password))
    if inta == True:
        print("PIN not supported.")
    elif passlen != 3:
        print(f"Password less than or greater than 4 characters not supported.")
    else: break


dic = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k',
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u','v',
        'w', 'x', 'y', 'z']

os.system('cls')
os.system('color 02')
if not inta:
    guess = ""
    a, b, c, d= 0,0,0,0
    st = time.time()
    while (True):
        guess = ""

        g1 = dic[a]
        g2 = dic[b]
        g3 = dic[c]
        g4 = dic[d]
        guess = f'{g1}{g2}{g3}{g4}'
        # print(guess)
        sys.stdout.write(f'\rPassword Guessing: {guess}, Time: {time.time()-st:.2f}s')
        sys.stdout.flush()

        if (guess!=user_password):
            if d==25:
            if c==25:
                if b==25:
                    a+=1
                    b,c,d=0,0,0
                else:
                    b+=1
                    c=0
            else:
                c+=1
                d=0
        else:
            d+=1
    elif guess == 'star':
        print('star')
    else:
        et = time.time() - st
        break

sys.stdout.write(f"\rYour password is {guess}, Time Taken: {et:.2f}s")
time.sleep(2)
os.system('color 07')

运行几次后,检测到许多其他单词,如"aaaa", "rrrr", 。"tttt"但是这个脚本无法检测到这个词"star"

标签: pythonpython-3.x

解决方案


import os
import sys
import time
import string

while True:
    user_password = input("Enter your password: ")

    try:
        user_password = int(str(user_password))
        inta = True
    except Exception:
        user_password = str(user_password).lower()
        inta = False
    passlen = len(str(user_password))
    if inta == True:
         print("PIN not supported.")
    elif passlen != 4:
         print(f"Password less than or greater than 4 characters not supported.")
    else: break


dic = string.ascii_letters

os.system('cls')
os.system('color 02')
if not inta:
    cracked = False
    st = time.time()
    for g1 in dic:
        for g2 in dic:
            for g3 in dic:
                for g4 in dic:
                    if (g1+g2+g3+g4==user_password):
                        et = time.time() - st
                        cracked = True
                        print(f"\rYour password is {g1+g2+g3+g4}, Time Taken: {et:.2f}s")
                        break
                if cracked:
                    break
            if cracked:
                break
        if cracked:
            break

不是最好的,但它可以满足您的需求

Enter your password: star
Your password is star, Time Taken: 0.59s

推荐阅读