[Tutor] help with inch to cms conversion .

Danny Yoo dyoo at hashcollision.org
Mon Feb 11 17:48:03 CET 2013


On Mon, Feb 11, 2013 at 9:06 AM, Pravya Reddy <pravyareddy at gmail.com> wrote:
> Can you please help me with the code.
>
> #!/usr/bin/env python
> """
> inchtocm.py
>
> """
>
> def Inchtocm(inches):
>     """Returns 2.54 * inches"""
>     return (2.54 * float(inches_number1))


I don't know if your curriculum talks about writing test cases for
functions.  If your instructors do not mention it, ask them, because
it's a crucial concept.  And if they have no clue about it (which is
unfortunately possible), you probably will need to stretch a little to
learn about them yourself.



Python's default testing framework, unittest, unfortunately requires
knowledge on how to use "classes", which you haven't probably seen
yet.  And it's a bit heavyweight, to boot.

In lieu of that, you can use something simpler: just run your
Inchtocm() with a few sample inputs first, and "assert" that they have
the right value, like this:


    def Inchtocm(inches):
        """Returns 2.54 * inches"""
        return (2.54 * float(inches_number1))

    assert Inchtocm(0) == 0


Note that we don't just call Inchtocm(), but check to see that it has
the right value.  This is important: otherwise, you are not really
"testing" the function, but just making it run.  The code above just
has one test: usually, you want to add a few more.  And you want to
write them _before_ you code up the function.


More information about the Tutor mailing list