#! /usr/bin/env python3.2
# -*- coding: utf-8 -*-
# main.py
""" Task: Exercise 7.3
Sometimes when programmers get bored or want to have a bit of fun, they add
a harmless Easter Egg to their program
(en.wikipedia.org/wiki/Easter_egg_(media)).
Modify the program that prompts the user for the file name so that it
prints a funny message when the user types in the exact file name
’na na boo boo’. The program should behave normally for all other files
which exist and don’t exist. Here is a sample execution of the program:
python egg.py
Enter the file name: mbox.txt
There were 1797 subject lines in mbox.txt
python egg.py
Enter the file name: missing.tyxt
File cannot be opened: missing.tyxt
python egg.py
Enter the file name: na na boo boo
NA NA BOO BOO TO YOU - You have been punk'd!
We are not encouraging you to put Easter Eggs in your programs -
this is just an exercise.
"""
''' Functions '''
def enterFileName():
"""
The user has to enter a filename.
Returns fileName
"""
fileName = None
while fileName == None:
# Enter filename
try:
fileName = input("Enter the filename: ")
except:
print("Invalid input!")
continue
return fileName
# End of enterFileName()
''' Main '''
file = None
subjectCounter = 0
fileName = enterFileName()
try:
file = open(fileName, 'r') # Open file
except:
if fileName == "na na boo boo":
print("NA NA BOO BOO TO YOU - You have been punk'd!")
else:
print("File cannot be opened: " + str(fileName))
exit()
if file != None:
try:
for line in file: # Read file content
if line.startswith("Subject: "):
subjectCounter += 1
except:
print("Error on reading file content!")
print("There were " + str(subjectCounter) + " subject lines in " +
str(fileName))
try:
file.close() # Close file
except:
print("File could not be closed!")
exit()