当前位置:   article > 正文

Python编程 从入门到实践 练习9-13、练习9-14_pythone 9-14

pythone 9-14

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
'
运行

9-13输出

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
'
运行

9-14输出

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/992866?site
推荐阅读
相关标签
  

闽ICP备14008679号