如何从掷骰子中检测出某些数字?

时间:2015-06-12 20:53:14

标签: python python-3.x dice

我制作了一个基于文本的RPG,它使用骰子滚动来进行战斗系统。如果你得到1,你的攻击就会被没收。您可以继续滚动,直到获得一个,或键入'attack'。我做了一个骰子,我只是想知道如何检测卷是26。这是代码:

print ("Type roll to roll the dice")
rollKeeper = ("1")
while rollKeeper == ("1"):
    rollOn = input ( )
    if rollOn == ("roll"):
        import random
        def rollDice():
            damage = random.randint(1,6)
        damage = random.randint (1,6)
        print (damage)

所以我想让它通过2检测数字6,这就是它。我知道

if damage == ("1"):

但这已经是我游戏的一部分了。我只需要能够检测它是否是其他任何一个(最多6)。

2 个答案:

答案 0 :(得分:0)

将其转换为函数,因为它可以重复使用。

import random

def roll_die():
    return random.randint(1,6)

然后只是测试。

result = roll_die()
if result == 1:
    # forfeit attack however you're doing that
else:  # roll between 2-6
    # do whatever your game logic has you doing.

答案 1 :(得分:0)

尝试添加:

if 2 <= damage <= 6:
    #insert code here