Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 8.3
- Rewrite the guardian code in the above example without two if statements.
- Instead use a compound logical expression using the and logical operator
- with a single if statement.
- """
- # test.txt is equal to mbox.txt, but added following 3 lines somewhere:
- """
- >>> The next line fails with the original code:
- From xyz
- >>> because of there are more than one, but less than two words...
- """
- ''' Main '''
- try:
- fhand = open('test.txt')
- except:
- print("File not found!")
- exit()
- week = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
- count = 0
- for line in fhand:
- words = line.split()
- # print('Debug:', words)
- if len(words) >= 3 and words[0] == 'From' and words[2] in week[:]:
- try: # Additional guard
- print(words[2])
- count += 1 # counts the interesting lines
- except IndexError:
- continue
- print("There are " + str(count) + " interesting lines.")
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement