2016年11月24日木曜日

開発環境

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

TRY IT YOURSELF 9-13, 14.(No.5286)

コード(Emacs)

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

print('9-13.')
from collections import OrderedDict

d = OrderedDict()

d['word1'] = 'meaning1'
d['word5'] = 'meaning5'
d['word2'] = 'meaning2'
d['word4'] = 'meaning4'
d['word3'] = 'meaning3'

print('OrderedDict')
for k, v in d.items():
    print(k, v)

d1 = {}
d1['word1'] = 'meaning1'
d1['word5'] = 'meaning5'
d1['word2'] = 'meaning2'
d1['word4'] = 'meaning4'
d1['word3'] = 'meaning3'
print('dict')
for k, v in d1.items():
    print(k, v)

print('9-14')

import random


class Die:

    def __init__(self, sides=6):
        self.sides = sides

    def roll_die(self):
        n = random.randrange(1, self.sides + 1)
        print(n)

print('6-sided die')
die6 = Die()
for _ in range(10):
    die6.roll_die()

print('10-sided die')
die10 = Die(10)
for _ in range(10):
    die10.roll_die()

print('20-sided die')
die20 = Die(20)
for _ in range(10):
    die20.roll_die()

入出力結果(Terminal, IPython)

$ ./sample1.py
9-13.
OrderedDict
word1 meaning1
word5 meaning5
word2 meaning2
word4 meaning4
word3 meaning3
dict
word1 meaning1
word3 meaning3
word4 meaning4
word5 meaning5
word2 meaning2
9-14
6-sided die
5
6
1
3
4
3
4
5
4
1
10-sided die
10
8
5
2
5
5
6
5
3
5
20-sided die
10
17
7
4
10
18
8
11
2
1
$

0 コメント:

コメントを投稿

Comments on Google+: