我的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>"
答案 0 :(得分:0)
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":
答案 1 :(得分:0)
如果不知道你的想法和#34;正确的答案&#34;,谁能告诉你为什么他们没有做你期望的事情呢?他们肯定会做你所写的,所以我们必须大肆猜测你不理解的东西。
我的猜测是,您不了解and
和or
之间的优先顺序,因此您需要这样做:
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 …:
每当你遇到优先级问题时,尝试将其分解成碎片,然后你可以找出哪些部分需要括号,然后添加它们,然后重新构建大表达式。
答案 2 :(得分:0)
而不是尝试比较给定字符串的大写或小写字符的每个组合,一个简单的技巧是改为将小写字母小写并将其与您希望匹配的值的小写字母进行比较。
,而不是b1 == "BC" or b1 == "bc" or b1 == "Bc" or b1 =="bC"
只需使用b1.lower() == "bc"
如果total == 150
怎么办?你似乎没有这方面的案例。
而不是那么多elif
,将其分为四种情况
像这样:
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