python从入门到实践 9-13 使用OrderedDict、9-14 骰子

from collections import OrderedDict

glossary = OrderedDict()

glossary['string'] = 'A series of characters.'
glossary['comment'] = 'A note in a program that the Python interpreter ignores.'
glossary['list'] = 'A collection of items in a particular order.'
glossary['loop'] = 'Work through a collection of items, one at a time.'
glossary['dictionary'] = "A collection of key-value pairs."
glossary['key'] = 'The first item in a key-value pair in a dictionary.'
glossary['value'] = 'An item associated with a key in a dictionary.'
glossary['conditional test'] = 'A comparison between two values.'
glossary['float'] = 'A numerical value with a decimal component.'
glossary['boolean expression'] = 'An expression that evaluates to True or False.'

for word, definition in glossary.items():
    print("\n" + word.title() + ": " + definition)
from random import randint

class Die():
    def __init__(self,sides=6):
        self.sides = sides
    def roll_die(self):
        return randint(1,self.sides)
    
d6 = Die(6)

results = []
for i in range(10):
    result = d6.roll_die()
    results.append(result)
print('6-sided :')
print(results)

d10 = Die(10)
results = []
for i in range(10):
    result = d10.roll_die()
    results.append(result)

print('10-sided :')
print( results)

d20 = Die(20)
results = []
for i in range(10):
    result = d20.roll_die()
    results.append(result)
print('20-sided :')
print(results)

你可能感兴趣的:(python从入门到实践)