【问题标题】:IF Statement regarding tax (python/html)关于税收的 IF 声明 (python/html)
【发布时间】:2014-09-23 20:42:05
【问题描述】:

我的 If 语句不会给我任何正确的答案。有人可以看看吗?

if total > 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 ==
"bC":
    print"<p>Tax: $",(total-disc)*0.12,"</p>" elif total <150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
    print "<p>Tax: $",total*0.12,"</p>" elif total > 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
    print "<p>Tax: $",float(((total-disc)+giftwrp))*0.12,"</p>" elif total < 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
    print "<p>Tax: $",float((total+giftwrp))*0.12,"</p>" elif total > 150 and b1 != "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC":
    print"<p>Tax: $",float((total-disc))*0.12,"</p>" elif total < 150 and b1!= "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC":
    print "<p>Tax: $",float(total)*0.12,"</p>" elif total > 150 and b1 != "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC" and
form.getvalue("giftwrap"):
    print "<p>Tax: $",float(((total-disc)+giftwrp))*0.12,"</p>" elif total < 150 and b1 != "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC"
and form.getvalue("giftwrap"):
    print "<p>Tax: $",float((total+giftwrp))*0.12,"</p>"

这是其余的代码,谢谢:

import cgi
form = cgi.FieldStorage()

# print HTTP/HTML header stuff
print """Content-type: text/html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html><head>
    <title>Order Form</title>
    </head><body>
    """

    # print HTML body using form data
    print "<h1>Profesional Painters</h1>"
    print "<h2>Customer Reciept</h2>"
    print "<p>Customer Name:", form["customerName"].value, "</p>"
    print "<p>Customer Email Address:", form["customerEmail"].value, "</p>"
    print "<h2>Customer Address:</h2>"
    print "<p>Street:", form["customerAdd"].value, "</p>"
    print "<p>City:", form["customerCity"].value, "</p>"
    print "<p>Province:", form["customerProv"].value, "</p>"
    print "<p>Postal Code:", form["customerPostal"].value, "</p>"
    print "<h2>Payment Information:</h2>"
    print "<p>Card Type:", form["type1"].value, "</p>"
    print "<p>Card Number: XXXX-XXXX-XXXX-", form["four4"].value, "</p>"
    print "<p>Expiry Date:", form["expirt"].value, "</p>"

    print "<h2>Products Ordered</h2>"

    q1 = int(form["quantity"].value)*2
    q2 = int(form["quantity2"].value)*1
    q3 = int(form["quantity3"].value)*150
    q4 = int(form["quantity4"].value)*3
    q5 = int(form["quantity5"].value)*10

    if form.getvalue("interior"):
        print "<p>Interior Painting quantity:", form["quantity"].value, "</p>"
        print "<p>Cost: $" ,q1, ".00</p>"

    else:
        q1 = 0
        print "<p>Interior Painting quantity: 0 <br /> Cost: $0</p>"

    if form.getvalue("exterior"):
        print "<p>Exterior Painting quantity:", form["quantity2"].value, "</p>"
        print "<p>Cost: $" ,q2, ".00</p>"

    else:
        q2 = 0
        print "<p>Exterior Painting quantity: 0 <br /> Cost: $0</p>"

    if form.getvalue("pressure"):
        print "<p>Pressure Washing quantity:", form["quantity3"].value, "</p>"
        print "<p>Cost : $" ,q3, ".00</p>"
    else:
        q3 = 0   
        print "<p>Pressure Washing quantity: 0 <br /> Cost: $0</p>"

    if form.getvalue("wood"):
        print "<p>Wood Finishing quantity:", form["quantity4"].value, "</p>"
        print "<p>Cost: $" ,int(form["quantity4"].value)*3, ".00</p>"
    else:
        q4 = 0
        print "<p>Wood Finsihing quantity: 0 <br /> Cost: $0</p>"

    if form.getvalue("spraycan"):
        print "<p>Spray Can quantity:", form["quantity5"].value, "</p>"
        print "<p>Cost: $" ,int(form["quantity5"].value)*10, ".00</p>"
    else:
        q5 = 0
        print "<p>Spray Can quantity: 0 <br /> Cost: $0</p>"

    if form.getvalue("email"):
        print "<p>An email notification will be sent to ",form["customerEmail"].value, "</p>"

    total = q1 + q2 + q3 + q4 + q5
    print "<p>Total Cost of goods purchased is $: ", total

    def discount():
        return float(total*0.15)
    disc = discount()
    disc2=0
    if total > 150:
        print "<p>Discount: $" , float(disc),"</p>"
    else:
        total<150
        print "<p>Discount:" ,disc2,"</p>"

    g1 = int(form["quantity"].value)
    g2 = int(form["quantity2"].value)
    g3 = int(form["quantity3"].value)
    g4 = int(form["quantity4"].value)
    g5 = int(form["quantity5"].value)

    def gift():
        return g1+g2+g3+g4+g5 
    giftwrp = gift()
    if form.getvalue("giftwrap"):
        print "<p>Gift wrap cost: $ ",int(giftwrp),".00</p>"

    b1 = form["customerProv"].value


    total = q1 + q2 + q3 + q4 + q5

    if total > 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
        print"<p>Tax: $",(total-disc)*0.12,"</p>"
    elif total <150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
        print "<p>Tax: $",total*0.12,"</p>"
    elif total > 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
        print "<p>Tax: $",float(((total-disc)+giftwrp))*0.12,"</p>"
    elif total < 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":
        print "<p>Tax: $",float((total+giftwrp))*0.12,"</p>"
    elif total > 150 and b1 != "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC":
        print"<p>Tax: $",float((total-disc))*0.12,"</p>"
    elif total < 150 and b1!= "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC":
        print "<p>Tax: $",float(total)*0.12,"</p>"
    elif total > 150 and b1 != "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC" and form.getvalue("giftwrap"):
        print "<p>Tax: $",float(((total-disc)+giftwrp))*0.12,"</p>"
    elif total < 150 and b1 != "BC" or b1 != "bc" or b1 != "Bc" or b1 != "bC" and form.getvalue("giftwrap"):
        print "<p>Tax: $",float((total+giftwrp))*0.12,"</p>"


    print "</body></html>"

【问题讨论】:

  • 仔细阅读后,您的实际问题似乎是“有人可以看看吗?”所以答案是“是”。如果您有其他问题,您需要告诉我们它是什么。 if 声明是否与您预期的有所不同?好的,那你期望它做什么?您能否给我们几个简单的输入案例(totalb1 的值集以及您认为可能相关的所有其他内容)、预期结果(如“if 部分应该执行”)和实际结果(如“但它没有”)?更好的是,将其剥离为单线。
  • 您需要说出“正确答案”的样子。我们不是读心者。
  • 附带说明,float(…) 应该完成的所有工作是什么?如果要添加两个 float 值,则会得到一个 float。如果你添加两个int 值,你会得到一个int,但是将它乘以0.12 会得到一个float。如果你有字符串或其他东西,那么添加它们并在结果上调用float 不会有任何用处—float("10"+"20")float("1020") 相同,即1020.0,而不是30.0。如果它们是 decimal.Decimal 值,这可能很有用,但我怀疑它们不是。
  • 我想问的是:让我们说第一个 if 语句说:if total 150 and (b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC"): print "(total-disc)*0.12" 因为打折如果购买超过 150 美元,则适用。我希望 if 语句从折扣中减去总计,然后计算其税费 -
  • 最后几处提到form.getvalue("giftwrap",此选项用于礼品包装,如果用户选择此选项,礼品包装将在其完成后的总金额上添加打折。我什至不确定这是否是正确的放置方式

标签: python html forms


【解决方案1】:

or 运算符的优先级低于and 的优先级。这意味着表达式

if total > 150 and b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC":

被评估为

if (total > 150 and b1 == "BC") or b1 == "bc" or b1 == "Bc" or b1 == "bC":

你可能想要

if total > 150 and (b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 == "bC"):

也可以写成更易读的形式

if total > 150 and b1.lower() == "bc":

【讨论】:

  • 好吧,让我们说第一个 if 语句:if total
  • 如果总数 > 150 和 (b1 == "BC" 或 b1 == "bc" 或 b1 == "Bc" 或 b1 == "bC"),则第二个: print "( total-disc)*0.12" 因为如果购买超过 150 美元,则应用折扣。我希望 if 语句从折扣中减去总计,然后计算其税金
  • 最后几处提到form.getvalue("giftwrap",此选项用于礼品包装,如果用户选择此选项,礼品包装将在其完成后的总金额上添加打折。我什至不确定这是否是正确的放置方式
【解决方案2】:

在不知道您认为“正确答案”的情况下,谁能告诉您为什么他们没有按照您的预期做?他们肯定在做你写的东西,所以我们必须对你不理解的地方做出一个疯狂的猜测。

我的猜测是你不理解 andor 之间的优先级,所以你期待这个:

if total > 150 and b1 == "BC" or b1 == "bc" or …:

……意思是:

if total > 150 and (b1 == "BC" or b1 == "bc" or …):

但实际上,它的意思是:

if (total > 150 and b1 == "BC") or b1 == "bc" or …:

每当您遇到优先级问题时,请尝试将其分解为多个片段,然后您可以找出哪些片段可能需要括号,并添加它们,然后重新构建大表达式。

【讨论】:

  • 当我输入除这些组合之外的其他内容时,它仍然没有给我正确的答案,而且我的答案似乎不正确
  • @user3743933:由于您还没有告诉我们正确的答案是什么以及它的不同之处,我并不感到惊讶,我的疯狂猜测并不是您所希望的。
【解决方案3】:
  • 与其尝试对给定字符串的每个大小写字符组合进行比较,一个简单的技巧是将字符串小写并将其与您希望匹配的值的小写进行比较。

    即,而不是b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 =="bC"

    只需使用b1.lower() == "bc"

  • 如果total == 150 怎么办?你似乎没有这个理由。

  • 不用这么多elifs,把它分成四种情况

像这样:

if b1.lower() == "bc":
    if total > 150:
        # 1st case, is bc and total > 150
    else:
        # 2nd case, is bc and total <= 150
else:
    if total > 150:
        # 3rd case, is not bc and total > 150
    else:
        # 4th case, is not bc and total <= 150

【讨论】:

  • 这些都是很好的 cmets,但他们没有做任何事情来回答这个问题。
  • 公平地说,这个问题几乎没有给出答案应该是什么样子的任何迹象。究竟什么是“正确答案”? OP 几乎没有给出任何背景信息。
  • 是的,正如我在评论中所说,我认为对所写问题的唯一正确答案是,“是的,有人可以看看它。”或者也许“是的,我看过了。看起来像一堆 Python 代码。对你有好处。”所以,从字面上看,你的答案和任何答案一样好。但实际上,很难想象任何合理的 SO 问题都可以作为答案;这些只是关于如何使他的代码更具可读性的几个 cmets。
  • @abarnert 我想我只是希望用清晰的代码路径重写它会导致OP想要的任何正确的业务逻辑,自然地纠正自己;或 OP 能够更轻松地发现自己的错误。
猜你喜欢
  • 2014-07-24
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 2013-05-11
  • 2013-08-08
相关资源
最近更新 更多