为什么我的课不能正常上课?即使它们在只有一个类处于活动状态时工作

2024-06-17 13:58:04 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我的课程彼此不认识,但当让他们单独工作时,他们会像文章底部的参考打印一样工作。然而,存储和传输会在ram位置弹出,但是,它们在单独完成时工作良好,正如底部的“咨询”操作所示

<;功能出纳。存款地址:0x0000023A9B656048>

import datetime

now = datetime.datetime.now()


class account:
    owner = "name"
    pin = "1823"
    balance = 800

    def __init__(self, transfer, withdraw, deposit, consult):
        self.transfer = transfer
        self.withdraw = withdraw
        self.deposit = deposit
        self.consult = consult


class cashier:
    def __init__(self, withdraw, deposit, transfer, consult):
        self.transfer = transfer
        self.consult = consult
        self.withdraw = withdraw
        self.deposit = deposit

    def deposit(self):
        print("Please type pin to proceed")
        if account.pin == 1823:
            print("who would you like to send money to?")
        else:
            print("Invalid pin")

    def transfer(self):
        pass

    def withdraw(self):
        withdrawal = input("How much money do you want to withdraw? (there is a limit up to 10,000$ a day!)")
        account.balance -= int(withdrawal)

        if int(withdrawal) > 10000:
            print("withdrawals cannot be larger than 10,000 a day!")
            exit()
        elif int(withdrawal) > account.balance:
            print("your account does not have enough funds to complete your transaction")
        else:
            print("Transaction succesfull!\nPlease collect your money")


print('Today is', now)
print("Hello %s!\nWhat can I do for you today?" % account.owner)
action = input("Use commands such as withdraw, deposit, transfer or consult to continue!\n")

if action == "withdraw":
    print(cashier.withdraw)
if action == "consult":
    print("Your account's balance is %s $" % str(account.balance))
if action == "transfer":
    print(cashier.transfer)
if action == "deposit":
    print(cashier.deposit)
else:
    print("Uknown command, exiting programm")
    exit()

Tags: toselfifdefpinactionaccounttransfer
2条回答

要使用类,需要像这样实例化它们

Cashier = cashier()//I don't think you should ask for the transfer details while instantiating. But you can do that by adding the parameter in the bracket

现在,您可以使用出纳变量上的函数,如:-

Cashier.deposit(deposit_money)

我认为,如果您不想实例化并且想要一个不实际存储数据的函数,您可以使用静态函数,但我不认为这是您想要做的

cashier的各种函数不返回任何内容,因此打印只是打印函数的字符串表示形式。您可以让函数返回如下字符串:

class Cashier:  # PEP8 calls for CamelCase here

    def deposit(self):
        pin = input("Please type pin to proceed")
        if Account.pin == pin:
            return "who would you like to send money to?"
        else:
            return "Invalid pin"

    def transfer(self):
        pass

    def withdraw(self):
        withdrawal = input("How much money do you want to withdraw? (there is a limit up to 10,000$ a day!)")
        Account.balance -= int(withdrawal)

        if int(withdrawal) > 10000:
            return "withdrawals cannot be larger than 10,000 a day!"
        elif int(withdrawal) > Account.balance:
            return "your Account does not have enough funds to complete your transaction"
        else:
            return "Transaction successful!\nPlease collect your money"

编辑:我删除了初始值设定项,因为它们什么都没做。您需要用cashier = Cashier()实例化出纳,然后用cashier.withdraw()调用Cashier函数

这里对类的工作方式(以及OOP的一般工作方式)有一个基本的误解。在继续之前,你应该通读一遍Python docs on building classes

相关问题 更多 >