Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Created January 13, 2026 07:07
Show Gist options
  • Select an option

  • Save pythonhacker/dde343e36b1dd88a68f1c49e495f4775 to your computer and use it in GitHub Desktop.

Select an option

Save pythonhacker/dde343e36b1dd88a68f1c49e495f4775 to your computer and use it in GitHub Desktop.
A Python cmd console representing rolling of a dice
import cmd
import random
import time
class DiceRoll(cmd.Cmd):
""" A custom command prompt for a rolling dice """
prompt = "dice>>> "
def __init__(self):
self.count = 0
self.total = 0
self.when = time.time()
self.last = 0
random.seed(self.when)
super().__init__()
def help_roll(self):
""" Help for roll """
print("Roll an unbiased dice and print the result")
def help_stats(self):
""" Help for stats """
print("Print dice rolling stats")
def do_roll(self, arg):
""" Roll dice and print result """
self.count += 1
number = random.randrange(1, 7)
self.total += number
self.when = time.time()
self.last = number
print(number)
def do_stats(self, arg):
""" Print stats so far """
print(f'Total rolls: {self.count}')
if self.count > 0:
last_roll = time.time() - self.when
avg = self.total/self.count
print(f'Avg roll: {avg:.2f}')
print(f'Last number: {self.last}')
print(f'Last roll: {round(last_roll)} seconds back')
def do_EOF(self, line):
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment