[Tutor] Python coding help
bob gailer
bgailer at gmail.com
Mon Oct 10 05:19:14 CEST 2011
On 10/8/2011 8:40 PM, Aisha Ali wrote:
> Hi,
>
> My computer science teacher provided optional Python exercises for us
> as we're learning about Java/Python right now. I decided to learn how
> to code these because I'm very interested in programming, but I don't
> know how to start on the problems in this case. Help? I'll love to
> learn more about Python!
>
> #############CODE YOUR FUNCTIONS HERE##############################
> # 0. Example:
> def f2c(tempF):
> """precondition: tempF is a number
> postcondition: returns tempF converted to centigrade"""
> return 5.0/9.0*(tempF - 32)
Above is an example of a complete Python function.
> # 1.
> def c2f(tempC):
> """precondition: tempC is a number
> postcondition: returns tempC converted to farenheit"""
> return 0
Above is an incomplete function which you are to complete - by replacing
return 0
with return erxpression-converting-c-to-f
Do you understand that much?
Can you complete the function?
ditto for 2 thru 10
This is backwards - here you are asked to do this before te above!
The idea is to write code to test your functions.
###################TEST CODE GOES HERE##############################
##Begin here! Look at your functions and develop tests before
##you create the function's code. This is your best protection
##against crash and burn.....
print "***********************Problem 0 Test Code:***********************"
t = 212
print "f2c(%5.4fs) = %5.4f, expected: %5.4f" %(t, f2c(t), 100)
t = -40
print "f2c(%5.4fs) = %5.4f, expected: %5.4f" %(t, f2c(t), -40)
t = 32
print "f2c(%5.4fs) = %5.4f, expected: %5.4f" %(t, f2c(t), 0)
The above are 3 calls to f2c which was defined in 0. way above.
They "test" f2c. The test code is complicated by the use of % for formatting. You might be better off doing it this way to avoid the complication of learning % formatting.
t = 212
print "f2(" + t +") = " + f2c(t) + " expected: 100"
Now you are to write similar tests for problems 1-10.
Enough to get you started?
--
Bob Gailer
919-636-4239
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111009/278ed62a/attachment.html>
More information about the Tutor
mailing list