# My name: # Date: # Assignment: Extra Credit 1 # My Collaboration Statement: #This import is needed for the getTemp function to work. import urllib #My functions go here: #A pre-built function that needs to be modified: def getHumidity(zipCode): if ( type(zipCode) != str ): print "Error! parameter must be a string!" return urlString = "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" + zipCode +"&wuSelect=WEATHER" filehandle = urllib.urlopen(urlString) #print the line with humidity text... for line in filehandle.readlines(): if("Humidity:" in line ): print line #You will have to figure out which line has the Humidity value, # pull it out of the line, convert it to a float, and then return the info. filehandle.close() #This function takes two parameters. The first is a function, and the second #is a list of test cases. If the function returns the correct values for #each test case, it passes. Otherwise, it fails! This function is a helper #function for the main test() function below. def testFunction(func,cases): allPassed = True for test in cases: result = func(test[0]) if result != test[1]: print "FAIL: Input:", test[0], "gives output:", result, "and I was expecting:",test[1] allPassed = False if (allPassed): print "PASSED all tests, Congrats!" #A test function that can be used to test student code: def test(): #Try testing the countJs() function: print "1. countJs()" try: testCases = [ ("Jay Summet",1), ("No big js",0), ("JJJJJJ",6) ] testFunction(countJs,testCases) except: print "Error: Unable to test the countJ's function!" #Try testing the maximum() function: print "2. maximum()" try: testCases = [ ( [5,6,7,8], 8), ( [7,5,4,1], 7), ( [], None), ( [True, False,5,"boo!"],"boo!")] testFunction(maximum,testCases) except: print "Error: Unable to test the maximum function!" #Try testing the getFloat function... # Note that the user must make sure this function worked! print "3. getFloat()" try: print "I am going to call the getFloat function now. Enter something that is not a valid number the first few times, then enter a valid number:" result = getFloat("Enter a number:") print "The function said you entered", result," if that is correct, it passes." except: print "Error: Unable to test the getFloat() function" #Try testing the isPrime function... print "4. isPrime()" try: testCases = [ (2,True), (3,True), (4,False), (5,True), (6,False), (7,True), (8,False), (9,False), (10,False), (11,True), (8387, True), (8000,False)] testFunction(isPrime,testCases) except: print "Error: Unable to test the isPrime() function!" #Try testing the getHumidity function (needs user help) print "5. getHumidity()" try: print "I'm calling the getHumidity funciton now. It should NOT print" print "anything to the screen. You should look up the Humidity for zip" print "code 98903 and make sure it matches the output below:" print "function returned:", getHumidity("98903") except: print "Error: unable to test the getHumidity() function!" #Try testing the changeGrade() function. print "6. changeGrade()" try: testCases = [ ("Jay","Jay"), ("Grade is B", "Grade is A"), ("ABCDEF","AABCED"), ("abcdef","abcdef"),("","")] testFunction(changeGrade,testCases) except: print "Error: Unable to test the changeGrade() function!" #Try testing the three countdown functions (user must help) print "7. various countdown functions!" try: print " You should see a countdown that looks like this from all three:" print "3" print "2" print "1" print "Done!" print "countdownWhile(3):" countdownWhile(3) print "countdownFor(3):" countdownFor(3) print "countdownRecursive(3):" countdownRecursive(3) except: print "Error: Unable to run all 3 countdown functions..." #try testing the decimalToBinary function: print "8. decimalToBinary()" try: testCases = [ (8,"1000"), (9,"1001"), (255,"11111111"), (0,"0")] testFunction(decimalToBinary,testCases) except: print "Error: Unable to test the decimalToBinary function."