Cash Register Lab (CodeGrade)

Close

Learning Goals


Key Vocab


Introduction

We're going to create an Object-Oriented Cash Register that can:


Instructions

This is a test-driven lab! You will need to read the spec file and the test output very carefully to solve this one.

Note that a discount is calculated as a percentage off of the total cash register price (e.g. a discount of 20 means the customer receives 20% off of their total price).

Hint #1: Keep in mind that to access an attribute or call an instance method inside another instance method, we use the self keyword to refer to the instance on which we are operating. For example:

class Person:

  def __init__(self, age=0):
    self.age = age

  def birthday(self):
    self.age += 1

Follow along with the tests in lib/testing/cash_register_test.py. Reading along with what the tests are looking for can be really helpful!

Take it one step at a time!

Hint #2: The apply_discount() method requires some knowledge about working with integers versus floats in Python. When you get to that method, take a look at what return value the tests are expecting and keep in mind that Python provides methods for changing an Integer to a Float and vice versa.

Hint #3: The void_last_transaction() method will remove the last transaction from the total. You'll need to make an additional attribute and keep track of that last transaction amount somehow. In what method of the class are you working with an individual item?

Hint #4: Python handles mutable default values for arguments differently than it handles immutable default values. This means that you should usually not set default values for lists, dictionaries, and instances of classes. You can learn more on this quirk in Python's documentation on More Control Flow Tools Links to an external site..