2014年8月6日水曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 5(Making Choices)、5.6(Exercises) 11.を解いてみる。

5.6(Exercises) 11.

コード(BBEdit)

sample11.py

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

for age, bmi in [(age, bmi) for age in range(44, 47) for bmi in range(21, 24)]:
    young = age < 45
    heavy = bmi >= 22.0

    if young and heavy:
        risk = 'medium'
    elif young and not heavy:
        risk = 'low'
    elif heavy:
        risk = 'high'
    else:
        risk = 'medium'

    print('age {age} bmi {bmi}: risk {risk}'.format(
        age=age, bmi=bmi, risk=risk))

入出力結果(Terminal, IPython)

$ ./sample11.py 
age 44 bmi 21: risk low
age 44 bmi 22: risk medium
age 44 bmi 23: risk medium
age 45 bmi 21: risk medium
age 45 bmi 22: risk high
age 45 bmi 23: risk high
age 46 bmi 21: risk medium
age 46 bmi 22: risk high
age 46 bmi 23: risk high
$

0 コメント:

コメントを投稿