Variation

If you remember back to the previous post, I said that I was inspired to make a bunch of different versions of diceroll.py to accomplish different things. One saved the dictionary of rolls to a file, one was built for speed, one only rolled d20s, and one was free of color and ASCII art the the Colorama disinclined.

diceroll-file.py

Working with files as a new programmer is a little intimidating. The last thing I’d want to do is somehow mess things up in a major way, so I always proceed very cautiously with file I/O operations. Once you get the hang of it though, it’s not so bad.

So the only difference here is just the last bit, where I tack the following onto the end of diceroll.py:

filename = set_name + '.txt'
f = open(filename, 'w')
f.write(str(roll_set))
f.close()

Pretty simple, nothing fancy at all. Not even taking each key:value pair in the dictionary and printing them out all nice-like. I figured it’d be easier to keep things this way in case you wanted to re-import the dict without any hassle and barely any code.

diceroll-quick.py

This variant strips down the original quite a bit. It takes out the naming mechanism (since it saves nothing to a dict) and the number-of-rolls option, giving the user just one value, making you run it again if you want another roll.

It still retains the input lines, however, so there’s no need to pass args straight to the program from the command line. This makes it self-contained and easy-to-use, and I like that.

diceroll-d20.py

This is the most stripped-down version. There’s no option to choose the number of dice, the sides of the dice, or even how much you’re adding on to it. Functionally, it’s pretty bare, and only really of use if there are absolutely no dice to be found. So to make it a little less boring, I included this line:

if roll == 20:
        print('CRITICAL!', end=', ')

Why? Because everyone loves a nat crit!

diceroll-boring.py

It’s boring because all it is is functionality. I took all of the pretty out of it. The user-input is still there though. Because I like it.

As always, if you want to comment on this, tweet me up @thunderchao.



Published

26 June 2012

Tags