如何检查用户是否存在于数据库列表中

时间:2017-11-09 17:22:41

标签: python-3.x

经过这么多个小时后,我仍然无法弄清楚如何检查我的数据中是否存在名称和密码用户输入。例如,当我问Please input customer name:并且他们输入Sam时我再次询问Please input customer password:并且输入为janos我希望调用customer_menu()函数。感谢

customers_list = []

class BankSystem(object):
    def __init__(self):
        self.customers_list = []
        self.load_bank_data()
    def load_bank_data(self):
        customer_1 = Customer("Sam", "janos", ["14", "Wilcot Street", "Bath", "B5 5RT"])
        account_no = 1234
        account_1 = Account(5000.00, account_no)
        customer_1.open_account(account_1)
        self.customers_list.append(customer_1)
    def customer_login(self, name, password):
        if name in customers_list and password in customers_list:
           self.name = name
           self.password = password
           self.customer_menu()
        else:
             print("sorry %s, it doesn't look like you are a customer"%name)
             exit()


    def main_menu(self):
        print ("1) Customer login")
        print (" ")
        option = int(input ("Choose your option: "))
        return option

    def run_main_option(self):
        loop = 1         
        while loop == 1:
            choice = self.main_menu()
            if choice == 1:
                name = input ("\nPlease input customer name: ")
                password = input ("\nPlease input customer password: ")
                msg = self.customer_login(name, password)
                print(msg)

person.py

class Person(object):

    def __init__(self, name, password, address = [None, None, None, None]):
        self.name = name
        self.password = password
        self.address = address

    def get_address(self):
        return self.address

    def update_name(self, name):
        self.name = name

    def get_name(self):
        return self.name

    def print_details(self):
        print("Name %s:" %self.name)
        print("Address: %s" %self.address[0])
        print("         %s" %self.address[1])
        print("         %s" %self.address[2])
        print("         %s" %self.address[3])
        print(" ")


    def check_password(self, password):
        if self.password == password:
            return True
        return False

    def profile_settings_menu(self):
        #print the options you have
         print (" ")
         print ("Your Profile Settings Options Are:")
         print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
         print ("1) Update name")
         print ("2) Print details")
         print ("3) Back")
         print (" ")
         option = int(input ("Choose your option: "))
         return option


    def run_profile_options(self):
        loop = 1           
        while loop == 1:
            choice = self.profile_settings_menu()
            if choice == 1:
                name=input("\n Please enter new name\n: ")
                self.update_name(name)
            elif choice == 2:
                self.print_details()
            elif choice == 3:
                loop = 0                     

customer.py

from person import Person

class Customer(Person):

    def __init__(self, name, password, address = [None, None, None, None]):
        super().__init__(name, password, address)

    def open_account(self, account):
        self.account = account

    def get_account(self):
        return self.account

    def print_details(self):
        super().print_details()
        bal = self.account.get_balance()
        print('Account balance: %.2f' %bal)
        print(" ")

1 个答案:

答案 0 :(得分:0)

     for customer in customers_list:
        if customer.name == name:
            if customer.password == password:
                ...