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)

return. If I just leave the assignment ofapples=0my code remains unaffected and I still receive a negative cost. Thanks