Last active
June 20, 2021 15:13
-
-
Save gaycookie/13884cb06f38332fb931ebbceccd8a9f to your computer and use it in GitHub Desktop.
Something random
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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