python实现骰子
运行结果: python dice.py ('change sides from 6 to ', 4, ' !')('change sides from 6 to ', 4, ' !')(4, 2)代码如下# Script Name : dice.py
# Author : Craig Richards
# Created : 05th February 2017
# La
预览截图
应用介绍
运行结果:
python dice.py
('change sides from 6 to ', 4, ' !')
('change sides from 6 to ', 4, ' !')
(4, 2)
代码如下
# 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())
©版权声明:本文内容由互联网用户自发贡献,版权归原创作者所有,本站不拥有所有权,也不承担相关法律责任。如果您发现本站中有涉嫌抄袭的内容,欢迎发送邮件至: www_apollocode_net@163.com 进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
转载请注明出处: apollocode » python实现骰子
文件列表(部分)
名称 | 大小 | 修改日期 |
---|---|---|
dice.py | 0.58 KB | 2020-02-25 |
发表评论 取消回复