Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Write a function printPositive that takes a string representing the name of a

ID: 639476 • Letter: 4

Question

4. Write a function printPositive that takes a string representing the name of an input file as a parameter. The input file contains numbers (integer or floating point), one per line. The function reads the numbers from the file and prints the values that are greater than zero to the screen, one per line. The following shows what the function would return when called on several sample files found in the zip file containing the assignment template: >>> printPositive (?nums1.txt?) 12 17.99 85.3 50 >>> printPositive (?nums2.txt?) 5 10 15 18.99 3.95 >>>

Explanation / Answer

def printPositive( filename ):
   with open(filename, 'r') as f:
       for line in f:
           if int(line) > 0:
               print line

printPositive("nums1.txt")

--------------------------------------

INPUT

10
20
-2
-3
21
-4
-3
3
3
-45

OUTPUT

10

20
21
3
3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote