Advertisement
evita_gr

Python 101 - Lesson 5 - Chapter 7

Oct 13th, 2011
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. # Exercise 7.1
  2. # Write a program to read through a file
  3. # and print the contents of the file (line by
  4. # line) all in upper case. Executing the
  5. # program will look as follows.
  6.  
  7. fname = raw_input('Enter a file name:')
  8.  
  9. try:
  10.   fhandle = open(fname)
  11. except:
  12.   print 'The file name(',fname,') you entered is wrong, try again.'
  13.   exit()
  14.  
  15. for line in fhandle:
  16.   print line.rstrip().upper()
  17.  
  18. fhandle.close()
  19.  
  20. # Exercise 7.2
  21. # Write a program to prompt for a file name
  22. # an then read through the file and look
  23. # for lines of the form: X-DSPAM-Confidence: 0.8475
  24. # When you encounter a line that starts with
  25. # "X-DSPAM-Confidence:" pull apart the line to
  26. # extract the floating point number on the line.
  27. # Count these lines and the compute the total of
  28. # the spam confidence values from these lines. When
  29. # you reach the end of the file, print out the
  30. # average spam confidence.
  31.  
  32. fname = raw_input('Enter the file name:')
  33.  
  34. try:
  35.   fhandle = open(fname)
  36. except:
  37.   print 'The file name(',fname,') you entered is wrong, try again.'
  38.   exit()
  39.  
  40. count = 0
  41. total = 0
  42.  
  43. for line in fhandle:
  44.   line = line.rstrip()
  45.   linesinuse = line.startswith('X-DSPAM-Confidence:')
  46.   if linesinuse:
  47.     count += 1
  48.     keep = line[20:]
  49.     total += float(keep)
  50.  
  51. div = float(total / count)
  52.  
  53. print 'Average spam confidence:',div
  54.  
  55. # Exercise 7.3
  56. # Sometimes when programmers get bored or want to
  57. # have a bit of fun, they add a harmless Easter Egg to
  58. # their program (en.wikipedia.org/wiki/Easter_egg_(media)).
  59. # Modify the program that prompts the user for the file
  60. # name so that it prints a funny message when the user
  61. # types in the exact file name ’na na boo boo’.
  62. # The program should behave normally for all other
  63. # files which exist and don’t exist. Here is a
  64. # sample execution of the program:
  65.  
  66. fname = raw_input('Enter the file name:')
  67.  
  68.  
  69. try:
  70.   fhandle = open(fname)
  71. except:
  72.   if fname == "na na boo boo":
  73.     print 'NA NA BOO BOO TO YOU - You have been punk\'d!'
  74.   else:
  75.     print 'The file name(',fname,') you entered is wrong, try again.'
  76.   exit()
  77.  
  78.  
  79. count = 0
  80. total = 0
  81.  
  82. for line in fhandle:
  83.   line = line.rstrip()
  84.   linesinuse = line.startswith('X-DSPAM-Confidence:')
  85.   if linesinuse:
  86.     count += 1
  87.     keep = line[20:]
  88.     total += float(keep)
  89.  
  90. div = float(total / count)
  91.  
  92. print 'Average spam confidence:',div
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement