#!/usr/bin/env python # -*- coding: utf-8 -*- # dice.py # A tool for RPG players -- throw dice quickly! # David Kolibáč (david@kolibac.cz) # 13. 3. 2010 # This software is public domain, hence it is free but without warranty. import random, sys, getopt # defaults sides = 6 times = 1 def throw(sides, times): """Get number of dice sides and how many times the dice should be thrown. Return a tuple of throw results.""" try: if sides < 4 or times < 1: raise ValueError except ValueError: "Number of sides must be 4+ and there must be at least one dice." return return [random.randint(1, sides) for i in range(0, times)] if __name__ == "__main__": try: opts, args = getopt.getopt(sys.argv[1:], "s:d:") except getopt.GetoptError, e: print "dice.py [-s sides] [-d dice]" sys.exit() for o, a in opts: if o == "-s": sides = int(a) elif o == "-d": times = int(a) print "%ik%i" % (sides, times) for i in throw(sides, times): print i,