当前位置:   article > 正文

python编程从入门到实践 答案 9-13 -- 9-15_ordereddict9-15

ordereddict9-15

9-13 使用OrderedDict

from collections import OrderedDict

favorite_languages = OrderedDict()

favorite_languages['ken'] = 'c'
favorite_languages['jack'] = 'PHP'
favorite_languages['ben'] = 'JAVA'
favorite_languages['phile'] = 'R'

for name, language in favorite_languages.items():
    print(name.title() + "'s favorite language is:" + language.title())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
'
运行

9-14 骰子

from random import randint

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

    def roll_die(self):
        self.sides = randint(1,6)
        print(self.sides)

    def roll_die_10(self):
        self.sides = randint(1,10)
        print(self.sides)

    def roll_die_20(self):
        self.sides = randint(1,20)
        print(self.sides)

die = Die(6)
print("6")
for i in range(0,10):
    die.roll_die()

print("10")
for i in range(0,10):
    die.roll_die_10()

print("20")
for i in range(0,10):
    die.roll_die_20()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
'
运行
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/992937
推荐阅读
相关标签
  

闽ICP备14008679号