2015年7月2日木曜日

開発環境

  • OS X Yosemite - Apple (OS)
  • Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
  • Python 3.4 (プログラミング言語)

Python for Kids: A Playful Introduction to Programming (Jason R. Briggs (著) 、No Starch Press)のPart Ⅰ.(Learning to Program)、Chapter 7.(Recycling Your Code with Functions and Modules)、Programming Puzzles(No. 1701)を解いてみる。

Programming Puzzles(No. 1701)

コード(Emacs, BBEdit)

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

print('#1: Basic Moon Weight Function')
def moon_weight(weight, inc):
    for year in range(16):
        print('{0:2} Earth weight: {1:.2f} Moon weight: {2:.2f}'.format(
            year, weight, weight * 0.165))
        weight += inc
moon_weight(30, 0.25)

print('#2: Moon Weight Function and Years')
def moon_weight(weight, inc, years):
    for year in range(years + 1):
        print('{0:2} Earth weight: {1:.2f} Moon weight: {2:.2f}'.format(
            year, weight, weight * 0.165))
        weight += inc
moon_weight(90, 0.25, 5)

print('#3: Moon Weight Program')
def moon_weight():
    s = input('Please enter your current Earth weight: ')
    weight = float(s)
    s = input('Please enter the amount your weight might increase each year: ')
    amount = float(s)
    s = input('Please enter the number of years: ')
    num = int(s)
    for year in range(num + 1):
        print('{0:2} Earth weight: {1:.2f} Moon weight: {2:.2f}'.format(
            year, weight, weight * 0.165))
        weight += amount
moon_weight()

入出力結果(Terminal, IPython)

$ ./sample.py
#1: Basic Moon Weight Function
 0 Earth weight: 30.00 Moon weight: 4.95
 1 Earth weight: 30.25 Moon weight: 4.99
 2 Earth weight: 30.50 Moon weight: 5.03
 3 Earth weight: 30.75 Moon weight: 5.07
 4 Earth weight: 31.00 Moon weight: 5.12
 5 Earth weight: 31.25 Moon weight: 5.16
 6 Earth weight: 31.50 Moon weight: 5.20
 7 Earth weight: 31.75 Moon weight: 5.24
 8 Earth weight: 32.00 Moon weight: 5.28
 9 Earth weight: 32.25 Moon weight: 5.32
10 Earth weight: 32.50 Moon weight: 5.36
11 Earth weight: 32.75 Moon weight: 5.40
12 Earth weight: 33.00 Moon weight: 5.45
13 Earth weight: 33.25 Moon weight: 5.49
14 Earth weight: 33.50 Moon weight: 5.53
15 Earth weight: 33.75 Moon weight: 5.57
#2: Moon Weight Function and Years
 0 Earth weight: 90.00 Moon weight: 14.85
 1 Earth weight: 90.25 Moon weight: 14.89
 2 Earth weight: 90.50 Moon weight: 14.93
 3 Earth weight: 90.75 Moon weight: 14.97
 4 Earth weight: 91.00 Moon weight: 15.02
 5 Earth weight: 91.25 Moon weight: 15.06
#3: Moon Weight Program
Please enter your current Earth weight: 90
Please enter the amount your weight might increase each year: 0.25
Please enter the number of years: 5
 0 Earth weight: 90.00 Moon weight: 14.85
 1 Earth weight: 90.25 Moon weight: 14.89
 2 Earth weight: 90.50 Moon weight: 14.93
 3 Earth weight: 90.75 Moon weight: 14.97
 4 Earth weight: 91.00 Moon weight: 15.02
 5 Earth weight: 91.25 Moon weight: 15.06
$

0 コメント:

コメントを投稿

Comments on Google+: