即使同时满足两个函数都为真的条件,第二个while循环也不会退出。
第二个循环类似于第一个循环,可以很好地工作,所以我不确定要怎么做。
def access(self):
print("Please enter your 5 digit account number:")
self.accountNumber = input()
while Customer.valueCheck(self.accountNumber) or Customer.accountNumberCheck(self.accountNumber) == False:
if Customer.valueCheck(self.accountNumber) == False:
print("The account number must be 5 digits only.")
elif Customer.accountNumberCheck(self.accountNumber) == False:
print("The account number given does not match the any on file.")
print("Please try again:")
self.accountNumber = input()
print("Please enter your account name:")
self.name = input()
while Customer.nameCheck(self.name) or Customer.accountHolderCheck(self.accountNumber, self.name) == False:
if Customer.nameCheck(self.name) == False:
print("The name must contain alphabetic characters only.")
elif Customer.accountHolderCheck(self.accountNumber, self.name) == False:
print("The name given does not match the name paired with that account number.")
print("Please try again:")
self.name = input()
print("")
return (self.name, self.accountNumber)
def accountHolderCheck(accountNumber, name):
with open("bank_data.txt", "r") as searchData:
for line in searchData:
line.strip().split("\n")
if line.startswith(accountNumber):
bank_search = line.strip().split(",")
if bank_search[1] == name:
break
else:
return False
输入正确的值后,它会要求不说任何打印语句而再次尝试,这意味着它们都正确,但是如果两个都正确,那么肯定不应该运行循环吗?
答案 0 :(得分:1)
您需要在两个条件前后加上括号。目前,您正在检查两种不同的情况:
Customer.valueCheck(self.accountNumber)
和
Customer.accountNumberCheck(self.accountNumber) == False
换句话说,第一个条件是检查该第一部分是否正确,而不是错误。因此,如果两个条件都为true,则循环将因为满足第一个条件而运行,但两个条件都不为假,因此您的最后一个print语句
print("Please try again:")
将被触发。
相反,使用:
while (Customer.valueCheck(self.accountNumber) and Customer.accountNumberCheck(self.accountNumber)) == False:
...
这将检查任一条件是否为假,如果两个条件都为真,则将不会运行。
答案 1 :(得分:0)
简短: 输入部分(print(“请重试:”)self.name = input())没有缩进。 长: 由于输入部分没有缩进,因此无论值正确与否,它都将始终运行。这是固定版本:
if Customer.nameCheck(self.name) == False:
print("The name must contain alphabetic characters only.")
self.name = input("Please try again: ")
elif Customer.accountHolderCheck(self.accountNumber, self.name) == False:
print("The name given does not match the name paired with that account number.")
self.name = input("Please try again: ")
此外,请查看thehumaneraser的贡献,因为这也是您需要解决的问题。 这是最终版本:
def access(self):
print("Please enter your 5 digit account number:")
self.accountNumber = input()
while Customer.valueCheck(self.accountNumber) == False or Customer.accountNumberCheck(self.accountNumber) == False:
if Customer.valueCheck(self.accountNumber) == False:
print("The account number must be 5 digits only.")
print("Please try again:")
self.accountNumber = input()
elif Customer.accountNumberCheck(self.accountNumber) == False:
print("The account number given does not match the any on file.")
print("Please try again:")
self.accountNumber = input()
print("Please enter your account name:")
self.name = input()
while Customer.nameCheck(self.name) == False or Customer.accountHolderCheck(self.accountNumber, self.name) == False:
if Customer.nameCheck(self.name) == False:
print("The name must contain alphabetic characters only.")
self.name = input("Please try again:")
elif Customer.accountHolderCheck(self.accountNumber, self.name) == False:
print("The name given does not match the name paired with that account number.")
self.name = input("Please try again:")
print("")
return (self.name, self.accountNumber)
def accountHolderCheck(accountNumber,name): 使用open(“ bank_data.txt”,“ r”)作为searchData: 对于searchData中的行: line.strip()。split(“ \ n”) 如果line.startswith(accountNumber): bank_search = line.strip()。split(“,”) 如果bank_search [1] ==名称: 打破 其他: 返回False