9-13 使用OrderedDict
from collections import OrderedDict
python_lists = OrderedDict()
python_lists['append'] = '将元素添加到列表末尾'
python_lists['insert'] = '可在列表的任何位置添加新元素'
python_lists['pop'] = '删除列表末尾的元素'
for word, mean in python_lists.items():
print(word + ":" + mean)
![9-13输出](https://i-blog.csdnimg.cn/blog_migrate/c3b4cd442b43b6444d7c0eb205b8e9a6.png)
9-14 骰子
from random import randint
class Die():
def __init__(self, sides = 6):
self.sides = sides
def roll_die(self):
"""打印位于1和骰子面数之间的随机数"""
x = randint(1, self.sides)
print(x)
six_die = Die()
for number in range(1,11):
six_die.roll_die()
ten_die = Die(10)
for number in range(1,11):
ten_die.roll_die()
twenty_die = Die(20)
for number in range(1,11):
twenty_die.roll_die()
![9-14输出](https://i-blog.csdnimg.cn/blog_migrate/55f2ba33f446a5fdb9228a1656b36d40.png)