# Script Name : dice.py# Author : Craig Richards# Created : 05th February 2017# Last Modified :# Version : 1.0# Modifications :# Description : This will randomly select two numbers,# like throwing dice, you can change the sides of the dice if you wishimport randomclass Die(object):# A dice has a feature of number about how many sides it has when it's# established,like 6.def __init__(self):self.sides = 6"""because a dice contains at least 4 planes.So use this method to give it a judgement when you needto change the instance attributes."""def set_sides(self, sides_change):if sides_change >= 4:if sides_change != 6:print("change sides from 6 to ", sides_change, " !")else:# added else clause for printing a message that sides set to 6print("sides set to 6")self.sides = sides_changeelse:print("wrong sides! sides set to 6")def roll(self):return random.randint(1, self.sides)d = Die()d1 = Die()d.set_sides(4)d1.set_sides(4)print(d.roll(), d1.roll())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。