gpt4 book ai didi

Python 字符串不相等

转载 作者:行者123 更新时间:2023-11-28 20:07:13 25 4
gpt4 key购买 nike

很抱歉,如果这个问题的答案可能很明显,但我是 Python 的新手(今天早上刚开始阅读关于 C 的不同结构和其他内容的小文档)。在练习的同时,我决定制作一台 ATM。然而,验证过程中发生了一些奇怪的事情,它将输入的 password 与代表用户数据库的 .txt 文件中的密码进行比较。尽管这两个字符串完全相等(是的,我检查了类型,都是 class str),但我的脚本完全无法正确比较这两个字符串!我正在寻找,我确信我遗漏了一些明显的东西,但我就是找不到。

这里是相关的部分:

class MockUserInterface:
def __init__(self):
ccn = input('Enter your Credit Card Number --> ')
password = input('Enter the corresponding password --> ')
self.db = MockDatabase()
self.processUser(ccn, password)

processUser(self, ccn, password) 将 ccn 和密码传递给 VerifyUser 以获取 False|dictionary 值...

class MockDatabase:
def __init__(self):
self.initdata = open('D:/users.txt', 'r')
self.data = {}
line = 0
for user in self.initdata:
line += 1
splitted_line = user.split(',')
self.data[splitted_line[0]] = {'Name' : splitted_line[1], 'Password' : splitted_line[2], 'Balance' : splitted_line[3], 'id' : line}
self.initdata.close()

def verifyUser(self, ccn, password):
if ccn in self.data:
if ccn == self.data[ccn]['Password']:
return self.data[ccn]
else:
print(password, self.data[ccn]['Password'])
else:
print(self.data)

users.txt 看起来像这样:

13376669999,Jack Farenheight,sh11gl3myd1ggl3d4ggl3,90001
10419949001,Sardin Morkin,5h1s1s2w31rd,90102
12345678900,Johnathan Paul,w3ll0fh1sm4j3sty,91235
85423472912,Jacob Shlomi,s3ndm35h3b11m8,-431
59283247532,Anon Tony,r34lp0l1t1k,-9999

运行脚本后,输出为:

C:\Python33\python.exe D:/PythonProjects/ATM(caspomat).py
Enter your Credit Card Number --> 13376669999
Enter the corresponding password --> sh11gl3myd1ggl3d4ggl3
sh11gl3myd1ggl3d4ggl3 sh11gl3myd1ggl3d4ggl3

Process finished with exit code 0

再次抱歉,如果答案很明显或者我没有提供足够的信息!

最佳答案

您正在将 ccn 与密码进行比较 - 而不是 password arg 与用户存储的密码...

if ccn == self.data[ccn]['Password']:

应该是

if password == self.data[ccn]['Password']:

关于Python 字符串不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com