Python 用户输入比较错误
我一直在用Python做这个简单的项目,如果用户需要访问脚本必须输入一个密钥,并且这个密钥存储在一个文本文件中,我已经构建了系统,它需要获取用户输入并将其与密钥和一切工作正常,就像如果输入错误密钥 3 次,程序将关闭,如果密钥正确,用户可以访问脚本,但问题是,有一个错误,我似乎无法弄清楚如何修复基本上是如果用户在被要求时按下回车键输入密钥(实际上没有写任何东西)脚本继续正常工作,我似乎无法理解为什么我需要一些帮助来尝试解决这个问题
脚本:
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
现在我似乎不知道为什么会发生这种情况可以自己测试一下,但我确保我的 .txt 文件中没有空格,并且单独按 Enter 时脚本仍然运行我希望任何人都可以帮助我解决这个问题,因为这让我发疯,我似乎不知道为什么
编辑:这是我的错我之前没有提到这一点,但有txt 文件中有多个键,这就是为什么我认为使用 in 会起作用,因为如果我使用 == 则不适用于每个单独的键
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您正在检查
if try_ in key:
对于 key 的任何子字符串都是 True 。例如,如果您有key = 'abc'
和try_= 'a'
,则try_ in key == True
。假设我正确理解了问题,您可能想要检查是否try_ == key
(只有当字符串相同时才会为 True)。The problem is that you are checking
if try_ in key:
which is True for any substring of key. For example if you hadkey = 'abc'
andtry_= 'a'
thentry_ in key == True
. What you probably want assuming I am understanding the question correctly, is to check iftry_ == key
(which will only be True if the strings are the same).可能有很多方法可以做到这一点。我只是添加了授予访问权限的变量并处理循环外部的一些逻辑。这是我的版本:
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: