Простой Python. Современный стиль программирования (Любанович) - страница 116

4. Запишите следующее стихотворение с помощью старого стиля форматирования. Подставьте строки 'roast beef', 'ham', 'head' и 'clam' в эту строку:

>My kitty cat likes %s,

>My kitty cat likes %s,

>My kitty cat fell on his %s

>And now thinks he's a %s.

5. Запишите следующее письмо по форме с помощью форматирования нового стиля. Сохраните строку под именем letter (это имя вы используете в следующем упражнении):

>Dear {salutation} {name},

>Thank you for your letter. We are sorry that our {product} {verbed} in your

>{room}. Please note that it should never be used in a {room}, especially

>near any {animals}.

>Send us your receipt and {amount} for shipping and handling. We will send

>you another {product} that, in our tests, is {percent}% less likely to

>have {verbed}.

>Thank you for your support.

>Sincerely,

>{spokesman}

>{job_title}

6. Создайте словарь с именем response, имеющий значения для строковых ключей 'salutation', 'name', 'product', 'verbed' (прошедшее время от глагола verb), 'room', 'animals', 'amount', 'percent', 'spokesman' и 'job_title'. Выведите на экран значение переменной letter, в которую подставлены значения из словаря response.

7. При работе с текстом вам могут пригодиться регулярные выражения. Мы воспользуемся ими несколькими способами в следующем примере текста. Перед вами стихотворение Ode on the Mammoth Cheese, написанное Джеймсом Макинтайром (James McIntyre) в 1866 году во славу головки сыра весом 7000 фунтов, которая была сделана в Онтарио и отправлена в международное путешествие. Если не хотите вводить это стихотворение целиком, используйте свой любимый поисковик и скопируйте его текст в программу. Или скопируйте его из проекта «Гутенберг» (http://bit.ly/mcintyre-poetry). Назовите следующую строку mammoth:

>We have seen thee, queen of cheese,

>Lying quietly at your ease,

>Gently fanned by evening breeze,

>Thy fair form no flies dare seize.

>All gaily dressed soon you'll go

>To the great Provincial show,

>To be admired by many a beau

>In the city of Toronto.

>Cows numerous as a swarm of bees,

>Or as the leaves upon the trees,

>It did require to make thee please,

>And stand unrivalled, queen of cheese.

>May you not receive a scar as

>We have heard that Mr. Harris

>Intends to send you off as far as

>The great world's show at Paris.

>Of the youth beware of these,

>For some of them might rudely squeeze

>And bite your cheek, then songs or glees

>We could not sing, oh! queen of cheese.

>We'rt thou suspended from balloon,

>You'd cast a shade even at noon,

>Folks would think it was the moon

>About to fall and crush them soon.