我不断收到此错误:AttributeError:模块'admin'没有属性'run_admin_options'

时间:2018-12-18 01:18:25

标签: python-3.x function class pycharm

运行程序时,我不断收到此错误。在bank_system文件中,给出了一个键入数字5的选项,因此它将run_admin_options函数加载到admin.py选项中。但是每次我运行程序时,都会出现此错误:

AttributeError: module 'admin' has no attribute 'run_admin_options'

我的admin.py模块中的代码:

class Admin:

    def __init__(self, fname, lname, address, house_number, road_name, town_city, postcode, user_name, password, full_rights):
        self.fname = fname
        self.lname = lname
        self.address = address
        self.house_number = house_number
        self.road_name = road_name
        self.town_city = town_city
        self.postcode = postcode
        self.user_name = user_name
        self.password = password
        self.full_admin_rights = full_rights

    def update_first_name(self, fname):
        self.fname = fname

    def update_last_name(self, lname):
        self.lname = lname

    def get_first_name(self):
        return self.fname

    def get_last_name(self):
        return self.lname

    def update_address(self, addr):
        self.address = addr

    def set_username(self, uname):
        self.user_name = uname

    def get_username(self):
        return self.user_name

    def get_address(self):
        return self.address

    # new

    def get_house_number(self):
        return self.house_number

    # new

    def update_house_number(self, house_number):
        self.house_number = house_number

    def get_road_name(self):
        return self.road_name

    def update_road_name(self, road_name):
        self.road_name = road_name

    # new

    def get_town_city(self):
        return self.town_city

    def update_town_name(self, town_city):
        self.town_city = town_city

    # new

    def get_postcode(self):
        return self.postcode

    def update_postcode(self, postcode):
        self.postcode = postcode

    def update_password(self, password):
        self.password = password

    def get_password(self):
        return self.password

    def set_full_admin_right(self, admin_right):
        self.full_admin_rights = admin_right

    def has_full_admin_right(self):
        return self.full_admin_rights

    def admin_account_menu(self):
        # print the options you have
        print()
        print()
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("Your admin options are:")
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("1) Update name")
        print("2) Update address")
        print(" ")
        option = int(input("Choose your option: "))
        return option

    def run_admin_options(self):
        loop = 1
        while loop == 1:
            choice = self.admin_account_menu()
            if choice == 1:
                fname = input("\n Enter new admin first name: ")
                self.update_first_name(fname)
                sname = input("\n Enter new admin last name: ")
                self.update_last_name(sname)

            elif choice == 2:
                house_number = input("\n Enter new admin house number: ")
                self.update_house_number(house_number)
                road_name = input("\n Enter new admin road name: ")
                self.update_road_name(road_name)
                town_city = input("\n Enter new admin town or city: ")
                self.update_town_name(town_city)
                postcode = input("\n Enter new admin postcode: ")
                self.update_postcode(postcode)
                print("Admin details successfully updated")

bank_system模块中的代码:

class BankSystem(object):
    def __init__(self):
        self.accounts_list = []
        self.admins_list = []
        self.load_bank_data()

    def load_bank_data(self):

        # create customers
        account_no = 1234
        customer_1 = CustomerAccount("Adam", "Smith", ["14", "Wilcot Street", "Bath", "B5 5RT"], account_no, 5000.00)
        self.accounts_list.append(customer_1)

        account_no += 5678
        customer_2 = CustomerAccount("David", "White", ["60", "Holborn Viaduct", "London", "EC1A 2FD"], account_no,
                                     3200.00)
        self.accounts_list.append(customer_2)

        account_no += 3456
        customer_3 = CustomerAccount("Alice", "Churchil", ["5", "Cardigan Street", "Birmingham", "B4 7BD"], account_no,
                                     18000.00)
        self.accounts_list.append(customer_3)

        account_no += 6789
        customer_4 = CustomerAccount("Ali", "Abdallah", ["44", "Churchill Way West", "Basingstoke", "RG21 6YR"],
                                     account_no, 40.00)
        self.accounts_list.append(customer_4)

        # create admins
        admin_1 = Admin("Taran", "Basi", ["224", "Kenpas Highway", "Coventry", "CV3 6PB"], 224, "Kenpas Highway", "Coventry", "CV3 6PB", "1", "pass", True)
        self.admins_list.append(admin_1)

    def search_admins_by_name(self, admin_username):
        # STEP A.2
        found_admin = None
        for a in self.admins_list:
            username = a.get_username()
            if username == admin_username:
                found_admin = a
                break
        if found_admin == None:
            print("\n ❌❌ The Admin %s does not exist! Please try again.\n" % admin_username)
        return found_admin

    def search_customers_by_name(self, customer_lname):
        # STEP A.3
        found_customer = None
        for c in self.accounts_list:
            lastname = c.get_last_name()
            if lastname == customer_lname:
                found_customer = c
                break
        if found_customer == None:
            print("\n The customer %s does not exist! Try again...\n" % customer_lname)
        return found_customer

    def main_menu(self):
        # print the options you have
        print()
        print()
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("Welcome to the Python Bank System")
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("1) Admin login")
        print("2) Quit Python Bank System")
        print(" ")
        option = int(input("Choose your option: "))
        return option

    def run_main_options(self):
        loop = 1
        while loop == 1:
            choice = self.main_menu()
            if choice == 1:
                username = input("\n Please input admin username: ")
                password = input("\n Please input admin password: ")
                msg, admin_obj = self.admin_login(username, password)
                print(msg)
                if admin_obj != None:
                    self.run_admin_options(admin_obj)
            elif choice == 2:
                loop = 0
        print("\n Thank-You for stopping by the bank!")

    def transferMoney(self, sender_lname, receiver_lname, receiver_account_no, amount):
        # ToDo
        pass

    def admin_login(self, username, password):
        # STEP A.1
        found_admin = self.search_admins_by_name(username)
        msg = "\n ❌ Login failed"
        if found_admin != None:
            if found_admin.get_password() == password:
                msg = "\n ✔ Login successful"
        return msg, found_admin

    def admin_menu(self, admin_obj):
        # print the options you have
        print(" ")
        print("Welcome Admin %s %s! Available options are:" % (admin_obj.get_first_name(), admin_obj.get_last_name()))
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("1) Transfer money")
        print("2) Customer account operations & profile settings")
        print("3) Delete customer")
        print("4) Print all customers detail")
        print("5) Update admin information")
        print("6) Sign out")
        print(" ")
        option = int(input("Choose your option: "))
        return option

    def run_admin_options(self, admin_obj):
        loop = 1
        while loop == 1:
            choice = self.admin_menu(admin_obj)
            if choice == 1:
                sender_lname = input("\n Please input sender surname: ")
                amount = float(input("\n Please input the amount to be transferred: "))
                receiver_lname = input("\n Please input receiver surname: ")
                receiver_account_no = input("\n Please input receiver account number: ")
                self.transferMoney(sender_lname, receiver_lname, receiver_account_no, amount)

            elif choice == 2:
                # STEP A.4
                customer_name = input("\n Please input customer surname :\n")
                customer_account = self.search_customers_by_name(customer_name)
                if customer_account != None:
                    customer_account.run_account_options()

            elif choice == 3:
                # STEP A.5
                customer_name = input("\n input customer surname you want to delete: ")
                customer_account = self.search_customers_by_name(customer_name)
                if customer_account != None:
                    self.accounts_list.remove(customer_account)
                    print("%s was successfully deleted!" % customer_name)

            elif choice == 4:
                # STEP A.6
                self.print_all_accounts_details()

            elif choice == 5:
                    admin.run_admin_options()

            elif choice == 6:
                loop = 0
                print("\n You have successfully logged out")

    def print_all_accounts_details(self):
        # list related operation - move to main.py
        i = 0
        for c in self.accounts_list:
            i += 1
            print('\n %d. ' % i, end=' ')
            c.print_details()
            print("------------------------")


app = BankSystem()
app.run_main_options()

任何人都可以通过此错误帮助我,我已经尝试了很多次以解决此问题,但是我根本没有运气,而且我不理解此错误的含义。所有这些代码让我感到困惑。

谢谢。

1 个答案:

答案 0 :(得分:1)

admin.run_admin_options()
BankSystem.run_admin_options中的

应该是

admin_obj.run_admin_options()