为什么脚本没有检测到“星”这个词?

2024-09-29 03:22:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个Python脚本来检查密码,但是这个脚本无法检测单词"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"


Tags: 脚本trueiftimeossyspassword单词
1条回答
网友
1楼 · 发布于 2024-09-29 03:22:20
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

相关问题 更多 >