Skip to content

Instantly share code, notes, and snippets.

@gaycookie
Last active June 20, 2021 15:13
Show Gist options
  • Save gaycookie/13884cb06f38332fb931ebbceccd8a9f to your computer and use it in GitHub Desktop.
Save gaycookie/13884cb06f38332fb931ebbceccd8a9f to your computer and use it in GitHub Desktop.
Something random
import json
from flask import Flask, request
from random import randrange
app = Flask(__name__)
def roll(type, rolls):
total = 0
for i in range(rolls):
total += randrange(4, 20) if type == 1 else randrange(1, 6)
return total
@app.post("/dice")
def dice():
data = request.json
rolls = roll(data['type'], data['rolls'])
return app.response_class(
response = json.dumps({ "result": rolls }),
mimetype = "application/json",
status = 200
)
import os, random
clear = lambda: os.system('cls' if os.name == 'nt' else 'clear')
def typeQuestion():
clear()
answer = input("What type of dice? [normal/dnd] -> ")
if answer not in ["normal", "dnd"]:
return typeQuestion()
else:
return answer
def countQuestion():
clear()
answer = input("How many dice? -> ")
try:
return int(answer)
except ValueError:
return countQuestion()
def startRolling(type, count):
total = 0
for i in range(count):
if type == "normal":
total += random.randrange(1, 6)
else:
total += random.randrange(4, 20)
return total
type = typeQuestion()
count = countQuestion()
total = startRolling(type, count)
print(f"Rolled {count} {type} dice with a total of {total}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment