#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """Never lose the dice again! The default is to print dice with a "x" as the dots, this can be changed by giving the first argument as a single character. """ from random import randint import sys def main(): """Parse sys.argv and send to print_dice """ if len(sys.argv) > 1: if sys.argv[1] in {"-h", "--help"}: print("Usage:", sys.argv[0], "[character]", "\n If character is given, use" " it as the 'dots' of the dice") sys.exit(1) assert len(sys.argv[1]) == 1, "only a single character can be given" character = sys.argv[1] else: character = None print("Hit 'Enter' to roll again, ^C or ^D to exit") print_dice(character) def print_dice(character): """Creat a loop and print random two random dice faces each time you hit enter, then exit if ^C or ^D is given """ line = " | {0} | | {1} |" top_bottom = " ------- -------" while True: try: die_one_side = side(randint(1, 6)) die_two_side = side(randint(1, 6)) if character != None: print(top_bottom, line.format(die_one_side[0], die_two_side[0]).replace("x", character), line.format(die_one_side[1], die_two_side[1]).replace("x", character), line.format(die_one_side[2], die_two_side[2]).replace("x", character), top_bottom, sep="\n") else: print(top_bottom, line.format(die_one_side[0], die_two_side[0]), line.format(die_one_side[1], die_two_side[1]), line.format(die_one_side[2], die_two_side[2]), top_bottom, sep="\n") input() # pause, wait for the user to hit Enter except (KeyboardInterrupt, EOFError): break # exit() def side(number): """Returns the dots relivant to number """ one = [" ", " x ", " "] two = ["x ", " ", " x"] three = ["x ", " x ", " x"] four = ["x x", " ", "x x"] five = ["x x", " x ", "x x"] six = ["x x", "x x", "x x"] numbers = [one, two, three, four, five, six] return numbers[(number - 1)] if __name__ == "__main__": main()