Advertisement
dougllio

readnumbers2.py

Apr 27th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. ######################################################################################
  2. # Simple file read/math
  3. #
  4. # A simple example using file read, modulo, for loop, if/else, and "input sanitization."
  5. # Basically reads in a comma separated list of values, and outputs count, sum, average,
  6. # and number of evens and odds. Also notes the invalid inputs.
  7. #
  8. # Tested on a file with the following data:
  9. # '100', '12', '22', '64', '2', '57', '99', '323', 'w', '6', '967', '3', '-2', '3.0', '5', '89', '32', '667', '3'
  10. ######################################################################################
  11.  
  12. # NOTE: A cleaner version of http://pastebin.com/kwtVP351
  13.  
  14. # Open the file into a file pointer
  15. fp = open("numbers.txt","r")
  16.  
  17. # Use the file pointer to read the contents of the file into a string variable
  18. text = fp.read()
  19.  
  20. # Close the file pointer
  21. fp.close()
  22.  
  23. # Use the split() function to separate the comma-separated text file into a list
  24. list = text.split(",")
  25.  
  26. # Output the list
  27. print "This is the list:",list
  28. print
  29.  
  30. # Initialize our statistics variables
  31. average = 0
  32.  
  33. # Loop through the list of numbers and process each number
  34. # Validate for whole numbers only
  35. sanitizedList = [int(item) for item in list if item.isdigit()]
  36. errorList = [item for item in list if not (item.isdigit())]
  37.  
  38. # Test for evens and odds using the modulo operation
  39. # We will calculate the number of odds using subtraction
  40. evensList = [item for item in sanitizedList if item % 2 ==0]
  41.  
  42. # Check for any errors
  43. if len(errorList)>0:
  44.     # Output an error message
  45.     print "Non whole numbers detected:",errorList
  46.  
  47. # If there was more than one valid value, calculate the average using the sum and count variables
  48. # Explicitly convert one value to float so that we get a more accurate average
  49. # Without this, the value would be rounded to the nearest whole number.
  50. listCount = len(sanitizedList)
  51. if listCount>0:
  52.   average = float(sum(sanitizedList))/listCount
  53.  
  54.  
  55. #Output the results of the calculations
  56. print
  57. print "File Read Statistics"
  58. print "--------------------"
  59. print "Total number of whole numbers:",listCount
  60. print "Sum of whole numbers:", sum(sanitizedList)
  61. print "Average of whole numbers:", average
  62. print "Total number of even whole numbers:", len(evensList)
  63. print "Total number of odd whole numbers:", listCount - len(evensList)
  64. print "Total number of input file errors:", len(errorList)
  65. print
  66. raw_input("Press Enter to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement