Home > Net >  Python user-input compare bug
Python user-input compare bug

Time:03-18

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:
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

Thanks in advance

CodePudding user response:

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).

CodePudding user response:

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()
                

CodePudding user response:

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()
  • Related