I'm trying to empower users to deposit/withdraw cash, and I would like to be able to do this all on one page. I created two radio buttons that the user can check to decide if they want to deposit or to withdraw, but I'm having a hard time checking if any button is checked at all, but also to know whether I should deposit or withdraw the amount and update their cash accordingly.

When I looked up this question I found a lot of ways to do this in javascript but I couldn't find any in python except maybe this which I have tried to follow, but it doesn't seem to be working for me. My two problems are:

1) Whether I click deposit or withdraw, it ends up subtracting in the amount entered in the users account.

2) If I select neither radio button, my code does not catch this and instead crashes.

How can I check for these?

deposit method (handles deposit and withdraw):

@app.route("/deposit", methods=["GET", "POST"])
def deposit():
"""Update cash amount."""

# check if user is submitting
if request.method == "POST":

    # make sure user provides an amount
    if not request.form.get("amount"):
        return apology("must provide amount")

    # make sure amount is valid
    if (request.form.get("amount").isalpha() or float(request.form.get("amount")) <= 0):
        return apology("invalid amount")

    amount = request.form.get("amount")
    option = request.form["option"]

    # try to check if no radio button was pressed
    if not option:
        return apology("must choose deposit or withraw")

    # depositing cash adds amount, withdrawing subtracs amount
    if option == deposit:
        db.execute("UPDATE users SET cash = cash + :amount WHERE id = :userId", amount = amount, userId = session["user_id"])
        db.execute("UPDATE users SET cash = cash - :amount WHERE id = :userId", amount = amount, userId = session["user_id"])

    # redirect user to home page
    return redirect(url_for("index"))

    return render_template("deposit.html")


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
    <form action="{{ url_for('deposit') }}" method="post">
        <!-- makes two readio buttons with different values so I can know which the user picked -->
        <div class="form-group">
            <input type="radio" name="option" value="deposit"> Deposit
        <div class="form-group">
            <input type="radio" name="option" value="withdraw"> Withdraw
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="amount" placeholder="Amount ($)" type="float"/>
        <div class="form-group">
            <button class="btn btn-default" type="quote">Confirm</button>
{% endblock %}

1 Answer 1



2) Add the required attribute to the radio buttons, then it will not let the user proceed without selecting one. Code is crashing here option = request.form["option"]. If no "option" is selected, it is not included in the POST body, so request.form["option"] doesn't exist. You can check out the python doc on get to see why option = request.form.get("option") does not crash.

1) Is this if option == deposit: what you mean? Isn't 'deposit' a string literal?

  • Thanks for answering, the required attribute worked perfectly. What I was trying to do there was check what the option the user picked so I could update the cash accordingly. I interpreted the answer to (link)this question that I could get the value of what which option the user picked and the compare it to the value of option one which is deposit and proceed from there. Also I tried to exchange the square brackets with parentheses and got error: TypeError: 'ImmutableMultiDict' object is not callable Jul 6, 2017 at 23:28
  • You need to add the .get method as well as changing the brackets. Jul 6, 2017 at 23:34
  • Oh yeah I'm an idiot sorry I didn't see the request form part. Well I tried that, but as before it seems to think that option == deposit is always false so it deducts every time. Jul 6, 2017 at 23:51
  • deposit needs quotes around it. Jul 6, 2017 at 23:55
  • Wew that was a tough bar to beat, but I managed. Out of curiosity, why wasn't that a syntax error? What was the computer comparing? Thanks a bunch. Jul 7, 2017 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .