Re: [Tutor] help with inch to cms conversion .
On Mon, Feb 11, 2013 at 9:06 AM, Pravya Reddy 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with inch to cms conversion .
On 02/11/2013 11:06 AM, Pravya Reddy wrote: Can you please help me with the code. #!/usr/bin/env python """ inchtocm.py """ First, remove that try/except until the code is free of obvious bugs. It's masking where the error actually occurs. Alternatively, include a variable there, and print the stack trace yourself. def Inchtocm(inches): """Returns 2.54 * inches""" return (2.54 * float(inches_number1)) As Joel pointed out, you're using a global instead of the parameter that was passed. Call float() on inches, not on some non-local variable. inches = None while True: try: inches_number1 = input(input("How many inches you want to convert: ")) Calling input(input()) doesn't do any favors. It echoes the first response, and waits for another one. The user doesn't probably realize that he has to type the number 455 twice. inches = float(inches_number1) Since you're presumably getting the exception on this line, you should print out the value you're trying to convert. You can remove the print after it works. print ("You got", Inchtocm(inches), "cm.") print ("You converted", inches, "inches to cm.") break except ValueError: print ("This is not a number!") The code is incomplete and i am not getting a proper output: How many inches you want to convert: 455 455 This is not a number! How many inches you want to convert: -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with inch to cms conversion .
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)) inches = None while True: try: inches_number1 = input(input("How many inches you want to convert: ")) inches = float(inches_number1) print ("You got", Inchtocm(inches), "cm.") print ("You converted", inches, "inches to cm.") break except ValueError: print ("This is not a number!") The code is incomplete and i am not getting a proper output: How many inches you want to convert: 455 455 This is not a number! How many inches you want to convert: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor