Python程式設計 從入門到實踐 練習9-13、練習9-14
阿新 • • 發佈:2021-01-25
9-13 使用OrderedDict
# 列印程式設計詞彙的含義
# 再新增5個python術語
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-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()