[Tutor] Fraction Class HELP ME PLEASE!

Cameron Simpson cs at zip.com.au
Thu Aug 6 23:44:32 CEST 2015

On 06Aug2015 16:55, Quiles, Stephanie <stephanie.quiles001 at albright.edu> wrote:
>I need to do the following assignment. I need to know how do i hard code an 
>example for each of the operators I am implementing? What i have so far is 
>below? He said he does not care if we plug in some numbers or if we have user 
>input numbers, however I am unsure of how to write a program that tests each 
>operator? Or can i write one that tests all of them? I don’t know where to 
>start with this. Please help!

For someone who doesn't know where to start, you seem to have a lot of decent 
looking code. Is the code below all yours? If so, a good start.

Also, please try to preserve the indentation when pasting in code; indentation 
is critical in Python as you know and incorrect indentation, when not an 
outright syntax error, can be a source of bugs. I'll presume your code was 
originally indented correctly.

Note that in this list we _do_ like code to be pasted inline where it can be 
seen directly and commented on like any other part of the message text. We're 
not big on attachments or links to external things (other than doco citations 
like your reference to the operators Python page).

>You may implement as many of these operators as you like (such as isub,
>itruediv, etc.) You MUST indicate in your header information which operators
>you are implementing, and you MUST hard code an example for each.

"Header information" is usually an opening comment. So perhaps start the 
program with text like this:

  # Here is a Fraction class implementing variaous operators. It currently
  # supports:
  # + (addition)
  # - (subtraction)
  # * (multiplication)
  # / (true division)

and so forth.

Regarding examples and tests, one workable approach to to make your program 
work as a "main program". The idea is that is invoked as a main program it will 
run the examples or tests.

As your code sits now it could be used as a module - someone could place it 
into python's library tree and import it, and use your Fraction class.  
However, you can of course run it directly at the command prompt, eg:

  % python your-fraction-file.py

When you do that, the code is still imported as a module but with the special 
name '__main__'. So what a lot of python modules do is have a "main" function 
which is to be fired only in the command line circumstance, such as:

  def main():
    F1 = Fraction(1, 2)     # 1/2
    F2 = Fraction(2, 3)     # 2/3
    print("F1 =", F1)
    print("F2 =", F2)
    print("F1 + F2 =", F1 + F2)
    print("F1 - F2 =", F1 - F2)
    print("F1 * F2 =", F1 * F2)

In order to make your program work as both an importable module which defines 
the Fraction class but does not run the main function and also as a main 
program which defines the class and then runs the main function you put this 
piece of boilerplate code on the very bottom of the program:

  if __name__ == '__main__':

which checks if you're invoking it directly from the command line. If so, run 
the main function.

Hopefully that gives you an idea about hardwiring examples also.

Regarding tests, you might write a simple function like this:

  def check(label, computed, expected):
    ok = computed == expected
    if ok:
      print(label, 'OK', computed, '==', expected)
      print(label, 'BAD', computed, '!=', expected)
    return ok

Then you might consider modifying your main program to run tests instead of 
bare examples, replacing:

  print("F1 + F2 =", F1 + F2)


  check("F1 + F2", F1 + F2, Fraction(7, 6))

Because check() returns whther the check was ok, you might even count the 
number of failures for some kind of report:

  fail_count = 0
  if not check("F1 + F2", F1 + F2, Fraction(7, 6)):
    fail_count += 1
  ... more checks ...
  print(fail_count, "failures")

and so forth.

This also gets you test code so that you can test your own program for 
correctness before submitting your assignment.

Feel free to return to this list with updated code and new questions.

Cameron Simpson <cs at zip.com.au>

More information about the Tutor mailing list