So I am writing some code to roll three fudge dice (six sided dice with sides of -1,-1,0,0,+1,+1). If the three dice together roll a total of -3, I have a function that then rolls a single fudge dice over and over again, subtacting 1 from the total for each -1 that get's rolled and quiting if something other than a -1 is rolled - in this way I get an "explosion down" making totals less than -3 possible, though increasingly less likely.
My explode down function is this:
def explodedown():
curval = -3
while 1:
newroll = rolldie()
if newroll != -1:
break
else:
curval = curval-1
return curval;
That seems to work well enough, but I almost feel that if I wanted to write this even more simply, there should be some way to write the loop more like:
while newroll == -1
newroll = rolldie()
curval = curval-1
And then the loop would naturally break without needing an if statement. Problem is newroll does not exist until we get inside the loop, so I don't think that will work. Maybe if I added another statement before the loop starts like:
newroll = rolldie()
while newroll == -1
newroll = rolldie()
curval = curval-1
But it seems un-pythonic to have the newroll line there twice.
Thoughts? is there a way to simplify and make more readable my explode down function?
You could do it like this:
while rolldie() == -1:
curval -= 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With