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
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')