if 语句(Python/HTML)
我正在尝试计算税费,但每次我执行条件语句时,它都会提示'BC'未定义。关于税的条件语句在代码的末尾。希望能得到一些帮助。以下是我的代码:
# 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>"
if total > 150 and form["customerProv"].value == BC or bc or Bc or bC:
print"<p>Tax: $",float((total-disc))*0.12,"</p>"
elif total <150 and form["customerProv"].value == BC or bc or Bc or bC:
print "<p>Tax: $",float(total)*0.12,"</p>"
elif total > 150 and form["customerProv"].value == BC or bc or Bc or bC and form.getvalue("giftwrap"):
print "<p>Tax: $",float(((total-disc)+giftwrp))*0.12,"</p>"
elif total < 150 and form["customerProv"].value == BC or bc or Bc or bC and form.getvalue("giftwrap"):
print "<p>Tax: $",float((total+giftwrp))*0.12,"</p>"
elif total > 150 and form["customerProv"].value != BC or bc or Bc or bC:
print"<p>Tax: $",float((total-disc))*0.12,"</p>"
elif total <150 and form["customerProv"].value != BC or bc or Bc or bC:
print "<p>Tax: $",float(total)*0.12,"</p>"
elif total > 150 and form["customerProv"].value != BC or bc or Bc or bC and form.getvalue("giftwrap"):
print "<p>Tax: $",float(((total-disc)+giftwrp))*0.12,"</p>"
elif total < 150 and form["customerProv"].value != BC or bc or Bc or bC and form.getvalue("giftwrap"):
print "<p>Tax: $",float((total+giftwrp))*0.12,"</p>"
print "</body></html>"
1 个回答
1
你提到了一些变量,比如 BC
、bc
等等:
if total > 150 and form["customerProv"].value == BC or bc or Bc or bC:
...但是你从来没有告诉 Python 这些变量是什么意思!所以 Python 就报错了。你是不是想用字符串呢?
if total > 150 and form["customerProv"].value == "BC" or "bc" or "Bc" or "bC":
不过,这样做也会失败。因为在 Python 中,你不能像这样连着用 or
。你应该分别检查每个值是否相等:
if total > 150 and form["customerProv"].value == "BC" or form["customerProv"].value == "bc" or form["customerProv"].value == "Bc" or form["customerProv"].value == "bC":
另外一个选择是,如果你想把一个值和一堆其他值进行比较,可以用下面这种(更整洁?)的代码:
if total > 150 and form["customerProv"].value in {"BC", "bc", "Bc", "bC"}:
...这段代码会检查这个值是否在那一组值里面。
在你的具体情况下,最简洁的方法是注意到你是在比较同一个字符串的不同大小写版本。如果你把要比较的字符串转换成大写,那么你就可以很简单地进行比较:
if total > 150 and form["customerProv"].value.upper() == "BC":
(由 Jon Clements 提出的建议)