mardi 28 mars 2017

Creating random integers with Flask and presenting them as a JSON object

I'm having some trouble and I'm lost on what to do with this. I need to create a Flask route which will simulate rolling two dice, then once the dice roll has been completed the output is a JSON object. I've been chipping away at this all morning and I'm hitting a brick wall. If anyone can offer me some help and/or suggestions I'd really appreciate it.

This is my code so far:

from flask import Flask
app = Flask(__name__)

import json
import random

@app.route('/')
def hello_world():
    return 'Hello World!'

# Create a route for rolling dice.
@app.route('/rolldice')
def dieobject(n):

    # Define roll count
    rollcount = []
    for i in range(n):

        # Define two dice rolls
        two_dieobject = random.randint(1, 6) + random.randint(1, 6)

        # Take output of pairdice and pass it to rollcount
    rollcount.append(two_dieobject)

    # Set rollcount as Json Data
    jsonData = '{rollcount}'
    jsonToPython = json.loads(jsonData)

    # Return Json data as the dice roll
    return 'jsontoPython'

if __name__ == '__main__':
    app.debug = True
    app.run()

Right now I'm getting a TypeError message stating that dieobject() takes 1 argument, 0 given. However beyond that error I'm not sure I know where I'm going with this code. Again if anyone can offer me some advice I'd really appreciate it, thanks!




Aucun commentaire:

Enregistrer un commentaire