0

The second while loop doesn't exit even when the condition, both functions being true, is satisfied.

The second loop is similar to the first one which works fine so I'm not entirely sure what to do.

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

When the correct value is entered it asks to try again without saying any of the print statements, implying they're both true, but if both are true then surely the loop shouldn't run?

Josh
  • 3
  • 2
  • Welcome to Stackoverflow! Please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), including some sample data that will run through your code and produce the same issue you are encountering. – Ian Thompson Aug 01 '19 at 18:00
  • Half of the relevant code is omitted, so there's no way to debug this ... As @IanThompson mentioned, please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – David Deprost Aug 01 '19 at 18:25

2 Answers2

1

You need to put parenthesis around your two conditions. At the moment you are checking two different conditions:

Customer.valueCheck(self.accountNumber)

and

Customer.accountNumberCheck(self.accountNumber) == False

In other words, the first condition is checking whether that first part is true, not false. So if both are true, the loop will run because the first condition is met, but neither will come up as false so your last print statement

print("Please try again:")

will be triggered.

Instead, use:

while (Customer.valueCheck(self.accountNumber) and Customer.accountNumberCheck(self.accountNumber)) == False:
...

This will check whether either condition is false, and will not run if both are true.

thehumaneraser
  • 632
  • 4
  • 21
0

Short: The input part (print("Please try again:") self.name = input())) is not indented. Long: Because the input part is not indented, it will always run whether the value is correct or not. Here's a fixed version:

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

Also, look at thehumaneraser's contribution, as that is also something you need to fix. Here's the final revision:

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

El El
  • 116
  • 4
  • Thanks, explained thehumanerasers bit better, although the indentations were intended – Josh Aug 01 '19 at 18:48