0

I am trying to do trigonometric calculations using python flask application by using query parameters. There are 2 query parameters angle and unit. I want to make unit an optional parameter so that suppose if I hit http://localhost:5000/trig/sin?angle=3 instead of http://localhost:5000/trig/sin?angle=3&unit=degree, it will also return the result sin(3).

@app.route("/trig/<func>")
def trig(func):
       if str(func) == "sin":
    try:
        angle = request.args["angle"]
        unit = request.args["unit"]

        if str(angle) == "" or str(unit) != "degree" or str(unit) != "radian":
            return "Invalid query parameter value(s)", 500
        else:
        sine = math.sin((math.radians(float(angle))))
        return "{0}".format(sine)

    except KeyError:
        return "Missing query parameter: angle", 500"
Parsa
  • 13
  • 3
  • Possible duplicate of [Why dict.get(key) instead of dict\[key\]?](https://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey) – Brad Solomon Feb 03 '19 at 13:58
  • I have tried None but it did not work. Giving me Bad Request. – Parsa Feb 03 '19 at 14:25

0 Answers0