Search code examples
pythonpython-3.xdice

How to detect certain numbers from a dice roller?


I have made a text-based RPG that uses dice rolling for the combat system. If you get a 1, your attack is forfeited. You can keep rolling until you get a one, or type 'attack'. I made a dice roller, and I just want to know, how to make it detect if the roll is 2 through 6. Here is the code:

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)

So I want to get it do detect numbers 2 through 6, and that's it. I know about

if damage == ("1"):

but that is already part of my game. I just need to be able to detect if it is any of the others (up to 6).


Solution

  • Roll it into a function, since it's reusable.

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

    then just test.

    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.