如何在同一类的第二个函数中引用变量?

问题描述 投票:0回答:1

如何正确定义和调用这些函数?

我正在尝试构建一个应用程序,该应用程序将提示主菜单,执行logon流程,转到下一个功能,即login menu,并引用logon功能的输入,以便用户执行不必输入他们的卡号和密码两次]。

我遇到的问题是试图能够引用位于同一类中的第二个函数中的变量。 [同事们告诉我使用全局变量是不好的,我不应该

。这是代码。

我已经删除了一些东西,因为它们并不是很重要。例如,我想使用elif choice ==3语句引用原始的one_row

ps.s。我已经更改def Login(self)以包含我要引用的变量,但是随后我的主菜单抱怨输入尚未定义。

class LoginPrompt:
    def Login(self):
        while True:
            print(menu[1])
            self.Card_number=str(input('>>  '))
            print(menu[2])
            self.Character_PINs = getpass.getpass('>>  ')
            self.one_row = c.execute('SELECT * FROM {tn} WHERE {cn}=? and {cnn}=?'.\
                            format(tn=table_1, cn=column_1, cnn=column_3),    (self.Character_PINs, self.Card_number,))
        for row in self.one_row.fetchone():
                print('Welcome: ', row)
                return

        else:
            print('PIN incorrect; try again')

    def loginMenu(self):
        while True:
            print(menu[5])
            print("\n1 - Deposit funds")
            print("2 - Withdraw funds")
            print("3 - Check balance")
            print("4 - Reset Pin")
            print("5 - Exit")

            while True:
                try:
                    choice = int(input("Please enter a number: "))
                except ValueError:
                    print("This is not a number")
                if choice >= 1 and choice <=5:
                    if choice == 1:
                        amount = input("\nPlease enter the deposit amount: ")
                        if amount != '' and amount.isdigit():
                            int(amount)
                            amount = c.execute('UPDATE {tn} SET Balances = ? WHERE {cn}=?'.\
                                                format(tn=table_1, cn=column_2), (amount,))
                        else:
                            print("Please enter a valid number")
                            conn.commit()
                            conn.close

                    elif choice ==3:
                        print(Login.one_row)

                    elif choice ==5:
                        input('Enjoy your stay, and always remember to drink Nuka Cola! ')
                        return(mainMenu)
                else:
                    return


def mainMenu():
        print(menu[0])
        chosen=False
        while not chosen:
        opt=int(input('\n Please choose one of the options below:\n\n-> Register for a new account [1]\n-> Login to an existing account [2]\n\nPlease type a number...\n\n>>  '))
        if opt==1:
            userReg()
            chosen=True
        elif opt==2:
            login_Menu = LoginPrompt()
            login_Menu.Login()
            chosen=True
            login_Menu.loginMenu()
        else:
            print('\n\nPLEASE TYPE EITHER 1 OR 2...\n ')
    print(chosen)
if __name__ == "__main__":
        while True:
            mainMenu()

如何正确定义和调用这些函数?我正在尝试构建一个应用程序,该应用程序将提示主菜单,通过登录过程,转到下一个功能,即登录...

python function class variables instance-variables
1个回答
0
投票

[这里,您实例化了LoginPrompt的一个实例,并在其上调用了LoginloginMenu

© www.soinside.com 2019 - 2024. All rights reserved.