简体   繁体   中英

Python user-input compare bug

I have been doing this simple project in python were if a user needs to access the script has to put a key and this key is stored in a text file and I have build the system were it needs to take the user input and compare it to the key and everything works fine like if a wrong key is entered 3 times the program shuts down and if the key is correct the user may access the script, but the problem is that there is a bug that I cant seem to figure out how to fix which basically is if the user just presses enter when asked to input the key (without actually writing anything) the script goes on working normally and I cant seem to understand why I need a bit of help trying to figure out how to fix this

The script:

key = open('key.txt').read()
def verify():
    for lit in range (3):
        print("=================================")
        try_ = input("[+]Please enter your key: ")
        if try_ in key:
            print("==================================")
            print("[+]Valid Entery \n[+]Access Granted")
            print("==================================")
            break
        elif try_ not in key:
            print("=================================")
            print("[+]Invalid Key \n[+]Access Denied")
            print("=================================")
            if lit == 2:
                time.sleep(0.8)
                print("======================================================")
                print("[+]Access Attempts Failed The Script Will Close Itself")
                print("======================================================")
                time.sleep(0.75)
                quit()




key.txt:
93TB-0OEP-7JKR-M2ZX
H8KN-5CAU-6P3M-TLFY
QBDO-LURT-VY2X-MP01
6H5U-IFS7-30GJ-P2NQ

Now I cant seem to know why this is happening anyone can test it out for themselves but I made sure there was no spaces in my.txt file and the script still runs when pressing enter alone I hope anyone can help me figure this out because this is driving me nuts and I cant seem to know why

Edit: This is my fault I did not mention this before but there is multiple keys in the txt file this is why I thought using in would work because if I used == that would not work for each individual key

Thanks in advance

The problem is that you are checking if try_ in key: which is True for any substring of key. For example if you had key = 'abc' and try_= 'a' then try_ in key == True . What you probably want assuming I am understanding the question correctly, is to check if try_ == key (which will only be True if the strings are the same).

import time
key1 = '6H5U-IFS7-30GJ-P2NQ'
key = open('key.txt').read()
"""
If you doubt that the key from the file is not equal to what 
you   have explicitly set, then you can do a check.
"""
print(key is key1)#If they are the same , then True

def verify():
    for lit in range (3):
        print("=================================")
        try_ = input("[+]Please enter your key: ")
        if try_ == key:
            print("==================================")
            print("[+]Valid Entery \n[+]Access Granted")
            print("==================================")
            break
        elif try_ not in key:
            print("=================================")
            print("[+]Invalid Key \n[+]Access Denied")
            print("=================================")
            if lit == 2:
                time.sleep(0.8)
                print("======================================================")
                print("[+]Access Attempts Failed The Script Will Close Itself")
                print("======================================================")
                time.sleep(0.75)
                quit()

verify()

There are probably many ways this can be done. I simply added the access granted variable and handled some of the logic outside of the loop. Here's my version:

import time

key = open('key.txt').read()

access_granted = 0

def verify():
    for lit in range(3):
        print("=================================")
        try_ = input("[+]Please enter your key: ")
        if try_ == key:        
            access_granted = 1
            break
        else:
            print("=================================")
            print("[+]Invalid Key \n[+]Access Denied")
            print("=================================")
    if access_granted == 0:
        time.sleep(0.8)
        print("======================================================")
        print("[+]Access Attempts Failed The Script Will Close Itself")
        print("======================================================")
        time.sleep(0.75)
        quit()
    else:
        print("==================================")
        print("[+]Valid Entry \n[+]Access Granted")
        print("==================================")

verify()
                

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM