1 Star 0 Fork 0

dzc/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dice.py 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
BhavKaran 提交于 2018-10-11 20:11 +08:00 . Add files via upload
# 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 wish
import random
class 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 need
to 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 6
print("sides set to 6")
self.sides = sides_change
else:
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())
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdredboy/Python-1.git
git@gitee.com:sdredboy/Python-1.git
sdredboy
Python-1
Python-1
master

搜索帮助