python BankAccount程序

时间:2016-07-18 17:40:14

标签: python

创建一个名为BankAccount的类

创建一个构造函数,该构造函数接受一个整数并将其赋值给balance属性。

创建一个名为deposit的方法,该方法收取现金存款金额并相应地更新余额。

创建一个名为withdraw的方法,该方法获取现金提取金额并相应地更新余额。如果金额大于余额返回"invalid transaction"

创建BankAccount类的子类MinimumBalanceAccount

嘿朋友我在网上学习python,我想尝试这个功课,但我有一些挑战来编译它,这是我的代码:

import unittest
class AccountBalanceTestCases(unittest.TestCase):
  def setUp(self):
    self.my_account = BankAccount(90)

  def test_balance(self):
    self.assertEqual(self.my_account.balance, 90, msg='Account Balance Invalid')

  def test_deposit(self):
    self.my_account.deposit(90)
    self.assertEqual(self.my_account.balance, 180, msg='Deposit method inaccurate')

  def test_withdraw(self):
    self.my_account.withdraw(40)
    self.assertEqual(self.my_account.balance, 50, msg='Withdraw method inaccurate')

  def test_invalid_operation(self):
    self.assertEqual(self.my_account.withdraw(1000), "invalid transaction", msg='Invalid transaction')

  def test_sub_class(self):
    self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg='No true subclass of BankAccount')

这是我从中得到的错误:

  

您的代码解决方案有错误

     

您的代码中存在错误/错误

     

结果:

Traceback (most recent call last):
  File "python/nose2/bin/nose2", line 8, in 
    discover()
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 300, in discover
    return main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 100, in __init__
    super(PluggableTestProgram, self).__init__(**kw)
  File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 133, in parseArgs
    self.createTests()
  File "/usr/local/lib/python2.7/dist-packages/nose2/main.py", line 258, in createTests
    self.testNames, self.module)
  File "/usr/local/lib/python2.7/dist-packages/nose2/loader.py", line 67, in loadTestsFromNames
    for name in event.names]
  File "/usr/local/lib/python2.7/dist-packages/nose2/loader.py", line 82, in loadTestsFromName
    result = self.session.hooks.loadTestsFromName(event)
  File "/usr/local/lib/python2.7/dist-packages/nose2/events.py", line 224, in __call__
    result = getattr(plugin, self.method)(event)
  File "/usr/local/lib/python2.7/dist-packages/nose2/plugins/loader/testclasses.py", line 119, in loadTestsFromName
    result = util.test_from_name(name, module)
  File "/usr/local/lib/python2.7/dist-packages/nose2/util.py", line 106, in test_from_name
    parent, obj = object_from_name(name, module)
  File "/usr/local/lib/python2.7/dist-packages/nose2/util.py", line 117, in object_from_name
    module = __import__('.'.join(parts_copy))
  File "/home/ubuntu/Applications/andelabs-server/tmp/5783854141a07c1400924f81-565b07b72568f41500779d20-test.py", line 4, in 
    from tmp.andelabs_5783854141a07c1400924f81_565b07b72568f41500779d20 import *
  File "/home/ubuntu/Applications/andelabs-server/tmp/andelabs_5783854141a07c1400924f81_565b07b72568f41500779d20.py", line 2
    def __init__(self,  balance = 90):
      ^
IndentationError: expected an indented block
请给我一些指导来纠正它。

1 个答案:

答案 0 :(得分:0)

您导入的模块似乎存在缩进问题。解决此问题的唯一方法是编辑文件本身并检查缩进,尤其是第2行,如错误消息中所述