【问题标题】:Poorly defined member function定义不明确的成员函数
【发布时间】:2020-08-22 02:16:11
【问题描述】:

作为测验的一部分,我正在在线编译器上运行以下内容。该代码工作正常并生成预期的输出,但是我无法通过所有测试条件。编译器在最后一次测试时产生错误。我已经重新检查了所有的拼写,但没有发现任何逻辑错误。

问题描述

假设类成员属性“用户名”设置为“管理员”,密码设置为“123”。然后,首先,登录函数验证用户名和密码。如果登录成功,则withdraw(amount) 询问用户数量,要提现,

condition-1:如果提款金额大于20000,则显示“提款资金不足”并返回“False”。

条件2:如果提款金额小于20000,则允许提款

错误/错误:

类方法'withdraw(amount) 未正确定义。检查函数名、平衡变量名和函数逻辑。

工作代码

class SavingAccount:
    balance = 20000
    username = ""
    password = ""

    def __init__(self,username,password):
        self.username=username
        self.password=password

    def login(self,username,password):
        if self.username == username and self.password == password:
            return True
        else:
            print("Invalid username or password")
            return False

    def withdraw(self, amount):
        if amount > self.balance:
            print("In-sufficient funds to withdraw")
            return False
        elif amount <= self.balance:
            self.balance = self.balance - amount
            return True

sobj=SavingAccount("Admin","123")
username = input("Enter your name to Login ")
password = input("Enter password ")
login = sobj.login(username,password)

if(login == True):

    amount = int(input("Enter amount to withdraw"))

    withdrawl = sobj.withdraw(amount)

    if withdrawl == True:
        print(amount,"is withdrew")
        print(sobj.balance,"balance amount")

这个问题在我研究所的私人服务器上可用,因此我附上代码和输出的屏幕截图。

【问题讨论】:

  • 你知道最后的测试是什么吗?例如,如果您知道错误的输入是什么,那么您可以单步执行该特定测试用例并查看发生了什么。你在公共场合建造这个东西吗?如果是这样,一个链接可能会帮助我们查看您是否缺少某些东西。我同意您的代码似乎符合基于一些快速测试的规范。
  • 其实这是在私人服务器上。我已经编辑了帖子并添加了整个问题的屏幕截图。如上图所示,我只收到了失败的测试用例消息。
  • 我认为 StackOverflow 可以帮助您的数量可能有限;您可以联系编写测试用例的人吗? (教授、老师、在线学习课程的支持?)他们可能能够提供更多帮助,并且更清楚地知道他们在寻找什么
  • 指令说资金不足的结果应该写成“不足”——但第三个样本输出没有那个连字符。在你的代码中尝试这种方式,也许?

标签: python python-3.x oop


【解决方案1】:

我已经替换了withdraw() 中的属性调用,它解决了问题。

这里是解决方案/更新withdraw()-

def withdraw(self,amount):
    if amount > SavingAccount.balance:
        print("In-sufficient funds to withdraw")
        return False
    elif amount <= SavingAccount.balance:
        SavingAccount.balance = SavingAccount.balance - amount
        return True

我想用SavingAccount.balance 代替self.balance 更优雅。

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 2011-06-17
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2012-08-07
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多