[Python] Checking if dice roll is between a range of numbers

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new to coding and am working on a text RPG. It will tell me whether or not I have a random encounter, but not what I come across. I am trying to add an encounter table with 2d6 and am having trouble understanding if statements and how to check between a range of numbers.

    import random

    Encounter = 5

    d20 = random.randint(1,20)
    print("your d20 rolled a" , d20)

    d6 = random.randint(1,6)
    #print("your d6 rolled a" , d6)

    d6x2 = d6 + random.randint(1,6)
    print("you rolled a", d6x2, "on two d6")

    if Encounter >= 5:
    if d20 >= 10:
    print("You have an encounter!")
    if d6x2 == (2,5):
    print("A guard catches you trying to escape!")
    elif d6x2 == (6,8):
    print("Thank goodness, it's just a rat.")
    elif d6x2 == (9,12):
    print("Oh great, another prisoner. I wonder if he wants to join you...")
    elif d20 < 10:
    print("You got lucky and don't have an encounter")
    else:
    print("You don't have an encounter")


    Even though d6x2 is returning values in the ranges stated the encounter text isn't printing. Why won't the if and elif text print?

    I have tried changing if d6x2 == (2,5): to if d6x2 == range(2,5).

    Continue reading...

Compartilhe esta Página