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

How to detect certain numbers from a dice roller?

我制作了一个基于文本的角色扮演游戏,它使用骰子滚动作为战斗系统。如果你得到 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)

所以我想让它检测数字 26,仅此而已。我知道

if damage == ("1"):

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

将其合并为一个函数,因为它是可重用的。

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.

尝试添加:

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