2

I'm supposed to be making a mock Grocery Store page that accepts user input, the number of items being "purchased". When you submit order, it takes you to a page where a bill is generated, showing: product, unit-cost, quantity ordered, and price.

I'm able to get this to work fine. However, the code as is, currently accepts a negative input, and will subtract cost. I want to modify the code, so that if an input is negative, the input is changed to 0. Thank you.

So I've tried a formal If statement:

if int(apples) < 0:
            apples=0
            return(apples)

With and without the return, but I'm getting nothing back.

However, whenever I do this nothing seems to happen. It continues to accept negative input and I receive no errors regarding this. I'll show the full code below, as the above tells nothing.

I've only tried the code for the apples variable so far, and have been unable to get it to work. I'm not sure what my mistake is:

def generate_bill():
        apples=request.form["num_gsmith"]
        if int(apples) < 0:
            apples=0
            return(apples)
        berries=request.form["num_strawberries"]
        eggs=request.form["num_eggs"]
        milk=request.form["num_milk"]
        soda=request.form["num_soda"]
        costApples=int(apples)*prices['apples']
        costBerries=int(berries)*prices['strawberries']
        costEggs=int(eggs)*prices['eggs']
        costMilk=int(milk)*prices['milk']
        costSoda=int(soda)*prices['soda']
        itemsToDisplay=[   # note there are four items in each list:
                                     # product name, price, number purchased, cost
                ["GrannySmith", prices['apples'], apples, costApples],
                ["Strawberries", prices["strawberries"], berries, costBerries],
                ["Eggs", prices["eggs"], eggs, costEggs],
                ["Milk", prices["milk"], milk, costMilk],
                ["Soda", prices["soda"], soda, costSoda]
                ]
        return render_template("bill.html",items=itemsToDisplay)

I had also tried converting the values to int in the input statement, but I keep getting the same results of no changes:

        apples=int(request.form["num_gsmith"])
        if apples <= -1:
            apples=0
            return(apples)

        berries=int(request.form["num_strawberries"])
        eggs=int(request.form["num_eggs"])
        milk=int(request.form["num_milk"])
        soda=int(request.form["num_soda"])
        costApples=apples*prices['apples']
        costBerries=berries*prices['strawberries']
        costEggs=eggs*prices['eggs']
        costMilk=milk*prices['milk']
        costSoda=soda*prices['soda']

So instead of using a negative number in the function for calculating the cost, the value is supposed to be converted to 0 once it is detected that it is a negative. Currently, I still get the negative number as seen in the bill image. Thank you.

I can't post images (not 10 rep)
but here is a direct link, if it helps visualize it at all:
https://imgur.com/a/4w7u43l

EDIT: to clarify, I'm using Spyder and FLASK This is my bill.html page

<html>
    <head><title>DLS Grocery - Your Bill</title></head>
<body>
<h1>Your Bill for DLS Grocery</h1>
<table border="2" width="100%">
<tr><th>Product</th><th>Unit Cost</th><th>Number Purchased</th><th>Cost</th></tr>
{% for element in items %}
   <tr>
       <td>
        {{ element[0] }}
        </td>
        <td>
        {{ element[1] }}
        </td>
        <td>
        {{ element[2]}}
        </td>
        <td>
        {{ element[3]}}
        </td>
    </tr>
{% endfor %}
 </table></body></html>

and this is my full page for my server.py:

from flask import Flask, render_template, request
import csv

app = Flask(__name__)
app.config["DEBUG"] = True
prices={"apples":0.79,"strawberries":1.99,"eggs":1.69,"milk":2.29,"soda":1.25}

@app.route("/generate_bill", methods=["POST"])
def generate_bill():
        apples=request.form["num_gsmith"]
        if int(apples) < 0:
            apples=0
            return apples
        berries=request.form["num_strawberries"]
        eggs=request.form["num_eggs"]
        milk=request.form["num_milk"]
        soda=request.form["num_soda"]
        costApples=int(apples)*prices['apples']
        costBerries=int(berries)*prices['strawberries']
        costEggs=int(eggs)*prices['eggs']
        costMilk=int(milk)*prices['milk']
        costSoda=int(soda)*prices['soda']
        itemsToDisplay=[   # note there are four items in each list:
                                     # product name, price, number purchased, cost
                ["GrannySmith", prices['apples'], apples, costApples],
                ["Strawberries", prices["strawberries"], berries, costBerries],
                ["Eggs", prices["eggs"], eggs, costEggs],
                ["Milk", prices["milk"], milk, costMilk],
                ["Soda", prices["soda"], soda, costSoda]
                ]
        return render_template("bill.html",items=itemsToDisplay)

if __name__ == '__main__':
    app.run(debug=True, port=5001)
10
  • You don't want return statement there at all. The real task is to render the template, correct? If you return, you never get to rendering the template. Commented Apr 1, 2019 at 16:30
  • Possible duplicate of Invert negative values in a list Commented Apr 1, 2019 at 16:31
  • 1
    @JanCarloOnce I'm not trying to turn the negative inputs positive, but rather convert them to 0. They are different, no? Commented Apr 1, 2019 at 16:35
  • @JohnSzakmeister I seem to run into the same issue when I don't use a return . If I just leave the assignment of apples=0 my code remains unaffected and I still receive a negative cost. Thanks Commented Apr 1, 2019 at 16:37
  • 1
    Have you tried doing a full refresh of the page? Perhaps it's getting cached? Have you tried examining the value of apples after you've set it to 0? Commented Apr 1, 2019 at 16:42

2 Answers 2

2

As far as I understand, The problem you are facing is because you are returning the program cursor. You should not return the program cursor, rather you should update the value. When you were returning the apple value then the page doesn't get render, hence you see wrong result, you code should look something like this.

from flask import Flask, render_template, request
import csv

app = Flask(__name__)
app.config["DEBUG"] = True
prices={"apples":0.79,"strawberries":1.99,"eggs":1.69,"milk":2.29,"soda":1.25}

@app.route("/generate_bill", methods=["POST"])
def generate_bill():
        apples=request.form["num_gsmith"]
        if int(apples) < 0:
            apples=0
        berries=request.form["num_strawberries"]
        eggs=request.form["num_eggs"]
        milk=request.form["num_milk"]
        soda=request.form["num_soda"]
        costApples=int(apples)*prices['apples']
        costBerries=int(berries)*prices['strawberries']
        costEggs=int(eggs)*prices['eggs']
        costMilk=int(milk)*prices['milk']
        costSoda=int(soda)*prices['soda']
        itemsToDisplay=[   # note there are four items in each list:
                                     # product name, price, number purchased, cost
                ["GrannySmith", prices['apples'], apples, costApples],
                ["Strawberries", prices["strawberries"], berries, costBerries],
                ["Eggs", prices["eggs"], eggs, costEggs],
                ["Milk", prices["milk"], milk, costMilk],
                ["Soda", prices["soda"], soda, costSoda]
                ]
        return render_template("bill.html",items=itemsToDisplay)

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

Output screenshot:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I had tried that initially but that did not have an affect. I still receive a negative display of cost. Thanks
1

By returning from generate_bill() after setting apples, you're actually sending the value of apples back as a result of the call (the template doesn't render, they just get a document that says "0"). That's not what you want. Perhaps try something more like this:

def get_positive(field):
    v = int(field)
    return max(v, 0)

@app.route("/generate_bill", methods=["POST"])
def generate_bill():
        apples = get_positive(request.form["num_gsmith"])
        berries = get_positive(request.form["num_strawberries"])
        eggs = get_positive(request.form["num_eggs"])
        milk = get_positive(request.form["num_milk"])
        soda = get_positive(request.form["num_soda"])

        costApples = apples * prices['apples']
        costBerries = berries * prices['strawberries']
        costEggs = eggs * prices['eggs']
        costMilk = milk * prices['milk']
        costSoda = soda * prices['soda']
        itemsToDisplay = [   # note there are four items in each list:
                             # product name, price, number purchased, cost
                ["GrannySmith", prices['apples'], apples, costApples],
                ["Strawberries", prices["strawberries"], berries, costBerries],
                ["Eggs", prices["eggs"], eggs, costEggs],
                ["Milk", prices["milk"], milk, costMilk],
                ["Soda", prices["soda"], soda, costSoda]
                ]
        return render_template("bill.html",items=itemsToDisplay)

Here get_positive() will convert the result to an int and ensure that it's greater than or equal to 0.

Ideally, you would catch other errors, such as request.form["num_gsmith"] not being numeric and return a good error. Right now, I believe you'd get a 500 error with the code you have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.