在Python中输入用户后尝试将负数转换为0

时间:2019-04-01 16:20:19

标签: python python-3.x

我应该制作一个模拟的杂货店页面,该页面接受用户输入,即“已购买”的商品数量。提交订单时,它将带您到生成账单的页面,显示:产品,单位成本,订购数量和价格。

我能够使它正常工作。但是,按原样编写的代码当前接受负输入,因此会扣除费用。我想修改代码,以便在输入为负数时将输入更改为0。谢谢。

所以我尝试了一个正式的If语句:

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

有回报也没有回报,但我什么也得不到。

但是,每当我这样做时,似乎什么都没有发生。它继续接受否定输入,因此我没有收到任何错误。我将在下面显示完整的代码,因为上面没有显示任何内容。

到目前为止,我只尝试了apples变量的代码,但无法使其正常工作。我不确定我的错误是什么

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)

我也曾尝试在输入语句中将值转换为int,但我不断得到相同的结果,但未做任何更改:

        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']

因此,在检测到成本为负数时,应该将值转换为0,而不是在函数中使用负数来计算成本。目前,帐单图片中仍显示负数。谢谢。

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

编辑:为了澄清,我正在使用Spyder和FLASK 这是我的bill.html页面

<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>

这是我的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)

2 个答案:

答案 0 :(得分:2)

据我了解,您面临的问题是因为您正在返回程序游标。您不应返回程序游标,而应更新该值。当您返回apple值时,页面将无法渲染,因此您看到错误的结果,您的代码应类似于以下内容。

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)

输出屏幕截图:

enter image description here

答案 1 :(得分:1)

通过设置generate_bill()后从apples返回,实际上是由于调用而将苹果的值发送回去(模板不呈现,它们只是得到一个说明“ 0”)。那不是你想要的。也许尝试这样的事情:

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)

此处get_positive()会将结果转换为int并确保其大于或等于0。

理想情况下,您会遇到其他错误,例如request.form["num_gsmith"]不是数字,并返回正确的错误。现在,我相信您的代码会出现500错误。