2016年7月10日日曜日

開発環境

Python Crash Course: A Hands-On, Project-Based Introduction to Programming (Eric Matthes (著)、No Starch Press)のPART1(BASICS)、Chapter 5.(If Statements)のTRY IT YOURSELF(No.2760)を取り組んでみる。

TRY IT YOURSELF(No.2760)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

print('5-3, 2, 3. Alien Colors #1, #2, #3')
for alien_color in ['green', 'yellow', 'red']:
    if alien_color == 'green':
        print('5 points')
    elif alien_color == 'yellow':
        print('10 points')
    elif alien_color == 'red':
        print('15 points')

print('5-6. Stages of Life')
for age in range(1, 70, 2):
    print(age, end=': ')
    if age < 2:
        print('baby')
    elif age < 4:
        print('toddler')
    elif age < 13:
        print('kid')
    elif age < 20:
        print('teenager')
    elif age < 65:
        print('adult')
    else:
        print('elder')

print('5-7. Favorite Fruit')
favorite_fruits = ['appla', 'banana', 'orange']
if 'banana' in favorite_fruits:
    print('You really like banans!')
if 'grape' in favorite_fruits:
    print('You really like grapes')

入出力結果(Terminal, IPython)

$ ./sample.py
5-3, 2, 3. Alien Colors #1, #2, #3
5 points
10 points
15 points
5-6. Stages of Life
1: baby
3: toddler
5: kid
7: kid
9: kid
11: kid
13: teenager
15: teenager
17: teenager
19: teenager
21: adult
23: adult
25: adult
27: adult
29: adult
31: adult
33: adult
35: adult
37: adult
39: adult
41: adult
43: adult
45: adult
47: adult
49: adult
51: adult
53: adult
55: adult
57: adult
59: adult
61: adult
63: adult
65: elder
67: elder
69: elder
5-7. Favorite Fruit
You really like banans!
$

0 コメント:

コメントを投稿

Comments on Google+: