打印报表仅显示列表中的第一个帐户名称

为了更清楚起见,我粘贴了完整的代码。我的问题是关于“withdrawal()”函数。尽管循环使用不同的帐户名称,但第二个打印语句中的 account.name 变量仅显示列表中的第一个帐户名称


def validation(accounts):

    pin = int(input("Enter 4 digits pin: "))

    for account in accounts:

        if pin == account.pin and len(str(pin)) == 4:

            print("\nWelcome! {}, your account balance is ${}".format(account.name, account.balance))

            return withdrawal(accounts)

    print("\nInvalid pin.\n")

    return try_again(accounts)


            

def withdrawal(accounts):

    amount = int(input("\nEnter amount to withdraw: "))

    for account in accounts:

        if account.balance > amount:

            account.balance -= amount

            print("\nTransaction successful, your new balance is ${}".format(account.balance))

            new = input("\nNew transaction? YES/NO?: ")

            if new.lower() == "yes":

                return withdrawal(accounts)

            print("\nTake your card {}. Thank you for banking with us.".format(account.name))

            break

    else:

        print("\nTransaction failed due to insufficient funds.")

                    

    

def try_again(accounts):

    re_enter = input("Enter YES to try again or NO to exit: ")

    if re_enter.lower() == "yes":

        return "\n" + validation(accounts)

    elif re_enter.lower() == "no":

        print("\nGoodbye. Take your card.")

    else:

        print("\nInvalid input. Take your card.")

    

        

class Account:

    def __init__(self, name, pin, balance):

        self.name = name

        self.pin = pin

        self.balance = balance

        


accounts = [Account("Bryan Somto", 4289, 300000), Account("Dubem Vic", 3329, 250000), Account("Munz Gee", 2200, 220000)]



validation(accounts)


蓝山帝景
浏览 76回答 1
1回答

慕容森

概括简单地说,这是因为每次withdrawal调用该函数时,它都会迭代所有帐户,并与第一个具有足够高余额的帐户进行交易。由于“Bryan Somto”是第一个帐户,因此交易始终通过该帐户进行。修改该withdrawal函数以仅接受用于进行交易的特定帐户。解释当您调用该withdrawal函数时,您应该只传递用户正在进行交易的特定帐户。因此,不要调用 ,而是withdrawal(accounts)调用withdrawal(account)。然后仅将该特定帐户传递给该函数。def withdrawal(account):    amount = int(input("\nEnter amount to withdraw: "))    if account.balance > amount:        account.balance -= amount        print("\nTransaction successful, your new balance is ${}".format(account.balance))                # New transaction        new = input("\nNew transaction? YES/NO?: ")        if new.lower() == "yes":            return withdrawal(account)                print("\nTake your card {}. Thank you for banking with us.".format(account.name))    else:        print("\nTransaction failed due to insufficient funds.")这里,该withdrawal函数仅处理特定帐户。如果您也修改您的功能,那就最好了validation。因为目前,如果多个人拥有相同的 PIN,则无法正常工作。它应首先输入帐户持有人的姓名,然后输入 PIN。然后它应该检查两者是否匹配。像这样:def validation(accounts):    name = input("Enter your name: ")    for account in accounts:        # Checking account name        if account.name == name:            pin = int(input("Enter 4 digits PIN: "))                        # Checking PIN length            if len(str(pin)) != 4:                print("\nInvalid PIN.\n")                return try_again(accounts)                        # Checking PIN            if account.pin == pin:                print("\nWelcome! {}, your account balance is ${}".format(account.name, account.balance))                return withdrawal(account)            else:                print("\nThe PIN is incorrect")                return try_again(accounts)    else:        print("\nThere is no account with that name.")        return try_again(accounts)这里它也只检查一次引脚的长度。在原始代码中,它每次都会检查长度,这是不必要的。if在函数的第一块中,如果改为try_again会更好。您不需要换行符,它可能会导致错误。return "\n" + validation(accounts)return validation(accounts)在旁边关于检查账户名:标准做法是使用一个绝对唯一的帐号/ID,那么即使两个人同名,它仍然有效。它也更好,因为通常输入帐号比输入长名称更容易。在这个例子中,如果两个人有相同的名字,它将选择列表中的第一个accounts,而永远不会到达第二个。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python