CSC111 Homework 6 2015

From dftwiki3
Revision as of 18:15, 1 March 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 16:18, 1 March 2015 (EST)


<showafterdate after="20150305 16:00" before="20150601 00:00">

Preparation


  • Create a program called testHw6.py with the code available on this page. Save it in the same place where you will store your hw6.py program.
  • Create a second program, called hw6.py. This program will contain all the functions that you are asked to write for this assignment. Add the code below to your hw6.py code:


# hw6.py
# yourname here
# This program contains functions that solve all the problems
# of Homework 6, including a function0() that is given as a 
# bonus.


# function0: receives a string, removes leading and trailing whitespace from
# it, and capitalize all its words, and add two stars at both ends.  Return the
# new string.
def function0( name ):
    name = "**" + name.strip().title() + "**"
    return name


# ================================================
#                    M A I N
# ================================================
def main():

    # --------------------------------------------------------
    # testing function0() with 3 different strings
    for name in [ "ARTHUR", "  smith COLLEGE  ", " lei   " ]:
        newName = function0( name )
        print( name )
        print( newName )
    print()



if __name__=="__main__":
   main()


  • The last 2 lines of the program are new. Don't worry about them, and just keep them in the code. All they do is call the main function when your program runs, the same way as before. The if __name__=="__main__": statement will be useful later.
  • The hw6.py program contains 1 function called function0() that is passed a string as a parameter, and returns a new string. The new string is created by taking the parameter, removing leading and trailing whitespace characters, capitalizing all the words in it, and adding 2 stars at both ends.
  • The main() function tests function0() by
  • calling it and passing it as a parameter one of 3 different strings ("ARTHUR", " smith COLLEGE ", " lei "),
  • catching the returned value in a variable called newName, and
  • printing the parameter and the returned value.
  • Run hw6.py and see how it works. Make sure you understand how main() tests function0().
  • Switch to testHw6.py (which you can keep open in another Idle window) and run it.
  • Here is the output you should get:


Function 0 passes 4 out of 4 tests.
Function1 does not exist or crashed. 0 out of 4 points.
Function2 does not exist or crashed. 0 out of 4 points.
Function3 does not exist or crashed. 0 out of 3 points.
Function4 does not exist or crashed. 0 out of 3 points.
Function5 does not exist or crashed. 0 out of 3 points.
Function6 does not exist or crashed. 0 out of 3 points.
Function7 does not exist or crashed. 0 out of 3 points.
Function8 does not exist or crashed. 0 out of 4 points.
Function9 does not exist or crashed. 0 out of 4 points.
Function10 does not exist or crashed. 0 out of 4 points.
Function11 does not exist or crashed. 0 out of 4 points.
Function12 does not exist or crashed. 0 out of 4 points.

Your functions pass 4 tests out of 47.


  • The output is telling you that function0() passed all 4 tests that testHw6.py subjected it to.
  • testHw6.py also indicates that Function1, Function2, all the way to Function12 were not found. This is normal: your hw6.py program only contains only function0(). Your assignment for this week is to code 12 different functions. Let's get started!


As you can see, you will have 2 different ways to test your program: 1) by calling your functions with different parameters in the main() function of hw6.py, or 2) by running testHw6.py, which will automatically import your program and call its functions, one after the other, with different parameters.


Function 1


  • Add a new function to your hw6.py program called function1(), with the following properties:
  • it has 1 parameter that is an integer or float number that represents a temperature in Celsius.
  • it returns the equivalent temperature in degrees Fahrenheit, defined as degree Fahrenheit = (degrees Celsius) * 9 / 5 + 32. The returned value is a float.


  • Add a new section to the main() program of your hw6.py that calls function1( ) and passes it some known temperatures, and print the returned value. For example:


# test function1() with -31, 0, 10, and 20 degrees Celsius
for celsius in [ -32, 0, 10, 20 ]:
     fahrenheit = function1( celsius )
     print( "celsius =", celsius, " fahrenheit  =", fahrenheit )


  • When you are satisfied that your program works, run testHw6.py to verify that it too finds function1() to run correctly. testHw6.py will report on whether it can run your function, and whether it works correctly.
  • If your function does not pass all the tests that testHw6.py subjects it to, you need to go back to hw6.py and see how you can modify function1().


Function 2


  • Repeat the same steps you took for function1(), and this time add a new function called function2() with the following properties:
  • function2()




</showafterdate>