2018年6月9日土曜日

開発環境

Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の5章(集合と確率を操作する)、5.4(プログラミングチャレンジ)、問題5-3(お金がなくなるまで何回硬貨を投げられるか)を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3
import random
import sys


def f(amount):
    count = 0
    while amount > 0:
        count += 1
        if random.randrange(2) == 0:
            amount += 1
            print(f'Head! Current amount: {amount}')
        else:
            amount -= 1.5
            print(f'Tails! Current amount: {amount}')
    return amount, count


if __name__ == '__main__':
    while True:
        while True:
            amount = input('Enter your starting amount: ')
            if amount == 'q':
                sys.exit(0)
            try:
                amount = int(amount)
            except Exception as err:
                print(type(err), err)
            else:
                break
        amount, count = f(amount)
        print(f'Game over :( Current amount: {amount}. Coin tosses: {count}')

入出力結果(Terminal, Jupyter(IPython))

$ ./sample3.py
Enter your starting amount: a
<class 'ValueError'> invalid literal for int() with base 10: 'a'
Enter your starting amount: 10
Tails! Current amount: 8.5
Tails! Current amount: 7.0
Head! Current amount: 8.0
Head! Current amount: 9.0
Tails! Current amount: 7.5
Tails! Current amount: 6.0
Tails! Current amount: 4.5
Head! Current amount: 5.5
Tails! Current amount: 4.0
Tails! Current amount: 2.5
Head! Current amount: 3.5
Tails! Current amount: 2.0
Tails! Current amount: 0.5
Head! Current amount: 1.5
Tails! Current amount: 0.0
Game over :( Current amount: 0.0. Coin tosses: 15
Enter your starting amount: 10
Head! Current amount: 11
Head! Current amount: 12
Head! Current amount: 13
Head! Current amount: 14
Head! Current amount: 15
Tails! Current amount: 13.5
Head! Current amount: 14.5
Tails! Current amount: 13.0
Tails! Current amount: 11.5
Tails! Current amount: 10.0
Head! Current amount: 11.0
Head! Current amount: 12.0
Head! Current amount: 13.0
Tails! Current amount: 11.5
Tails! Current amount: 10.0
Tails! Current amount: 8.5
Head! Current amount: 9.5
Tails! Current amount: 8.0
Tails! Current amount: 6.5
Tails! Current amount: 5.0
Head! Current amount: 6.0
Tails! Current amount: 4.5
Head! Current amount: 5.5
Tails! Current amount: 4.0
Head! Current amount: 5.0
Tails! Current amount: 3.5
Tails! Current amount: 2.0
Tails! Current amount: 0.5
Head! Current amount: 1.5
Head! Current amount: 2.5
Head! Current amount: 3.5
Head! Current amount: 4.5
Head! Current amount: 5.5
Head! Current amount: 6.5
Head! Current amount: 7.5
Tails! Current amount: 6.0
Head! Current amount: 7.0
Head! Current amount: 8.0
Head! Current amount: 9.0
Head! Current amount: 10.0
Head! Current amount: 11.0
Tails! Current amount: 9.5
Head! Current amount: 10.5
Tails! Current amount: 9.0
Tails! Current amount: 7.5
Head! Current amount: 8.5
Tails! Current amount: 7.0
Tails! Current amount: 5.5
Head! Current amount: 6.5
Head! Current amount: 7.5
Tails! Current amount: 6.0
Tails! Current amount: 4.5
Head! Current amount: 5.5
Tails! Current amount: 4.0
Tails! Current amount: 2.5
Tails! Current amount: 1.0
Head! Current amount: 2.0
Tails! Current amount: 0.5
Tails! Current amount: -1.0
Game over :( Current amount: -1.0. Coin tosses: 59
Enter your starting amount: 5
Tails! Current amount: 3.5
Tails! Current amount: 2.0
Head! Current amount: 3.0
Tails! Current amount: 1.5
Head! Current amount: 2.5
Tails! Current amount: 1.0
Tails! Current amount: -0.5
Game over :( Current amount: -0.5. Coin tosses: 7
Enter your starting amount: -10
Game over :( Current amount: -10. Coin tosses: 0
Enter your starting amount: 0
Game over :( Current amount: 0. Coin tosses: 0
Enter your starting amount: q
$

0 コメント:

コメントを投稿

関連コンテンツ