-2

I'm very new to coding and trying to write a simple program to roll dice for the TTRPG game Daggerheart (I play solo on paper, so something to help track both Fear and Hope seemed useful). In the game mechanic, two special dice (both d12) are defined, and if one rolls higher then the other (re: hope > fear) the player gets a resource ('hope'), or if the other way around, the GM gets a 'fear' resource. Now, the game allows these resources up to, but not exceeding 6.

So far, I can't figure out how to set this maximum for my 'hope' and 'fear' variables. The examples I've found on stackOverflow are either in another language (Java) or I don't know how to translate the solution to my situation.

This is my program so far. A snippet to increase hope to illustrate:

    # Roll the dice
    if playerMove == 'r':
        hopeDice = int(random.randint(1, 12))
        fearDice = int(random.randint(1, 12))
        roll = int(hopeDice) + int(fearDice)
        print('You rolled {} with ...'.format(roll))
        if hopeDice > fearDice:
            print('... hope')
            hope += 1
5
  • 5
    maybe you should check if hope <= 6: .... That's all. Commented Jul 12 at 21:07
  • 2
    Ya, you can't "set a maximum range on a variable". You need to just do a check before += to ensure that the increment is allowed. There's ways that you could abstract away the check (using a class for example), but you still need to do the check yourself at some point. Commented Jul 12 at 21:08
  • 1
    What happens if hope==6 and hopeDice > fearDice? Nothing? Commented Jul 12 at 21:11
  • 2
    Try set the cap like this - ``` hope = min(hope + 1, 6) # Cap at 6``` And no need to cast int(random....). Commented Jul 12 at 21:11
  • 2
    There is no automatic way to set a limit on a variable. You have to write code for that yourself. Commented Jul 13 at 1:28

1 Answer 1

3

You can't set a max value for a variable, but you can make sure to only increase the value if the variables is less than the max.

You can do it with an if statement

if hopeDice > fearDice:  
    if hope < 6:  
        #this block only runs if hope is between 0 and 5   
        hope = hope + 1  

Another way, as Daniel Hao commented, would be to always add 1 to the variable and set it to the minimum value between the result of variable+1 or your max value.

if hopeDice > fearDice:
    hope = min(hope+1, 6) # Returns 6 if hope+1>6

Suppose hope=6, on the next hope roll hope+1=7, then the function min returns the minimum value between 7 and 6.

In your code it would look like:

if playerMove == 'r':
    hopeDice = random.randint(1, 12)
    fearDice = random.randint(1, 12)
    roll = hopeDice + fearDice

    print('You rolled {} with ...'.format(roll))

    if hopeDice > fearDice:
        print('... hope')
        # Check if hope is less than max
        if hope < 6:
            hope = hope + 1
    if hopeDice < fearDice:
        print('... fear')
        if fear < 6:
            fear = fear + 1
    if hopeDice == fearDice:
        print('Doubles! An extreme event')
        print('%s Hope, %s Fear' % (hope, fear))

Notice I removed the int on roll = int(hopeDice) + int(fearDice) as per documentation randint

Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).


As a bonus, checking the code in you github, if you use hope while hope=0 would result in hope=-1. To prevent this you can check if you have hope available:

if playerMove == 'h':
    if Hope > 0:
        hope = hope - 1
        print('%s Hope, %s Fear' % (hope, fear))
    else:
        print('No hope available')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.