Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python3 

2 

3"""Solution to chapter 7, exercise 31: plfile""" 

4 

5 

6def plword(word): 

7 """Takes a string as input. It should be a single 

8word. Returns a string, the input word translated into 

9Pig Latin. 

10""" 

11 if word[0] in 'aeiou': 

12 return word + 'way' 

13 

14 return word[1:] + word[0] + 'ay' 

15 

16 

17def plfile(filename): 

18 """Takes a filename as input. Returns a string 

19containing the file's contents, with each word 

20translated into Pig Latin. 

21""" 

22 return ' '.join(plword(one_word) 

23 for one_line in open(filename) 

24 for one_word in one_line.split())