if语句(python / html)

时间:2014-07-31 09:57:00

标签: python html

我正在尝试计算税,但每当我尝试执行if语句时,它都会说'BC'没有定义。重新征税的if语句接近尾声。感谢任何帮助。继承我的代码: 导入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>"

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 个答案:

答案 0 :(得分:1)

您正在引用变量 BCbc等等:

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

如果你想将一个值与一堆其他值进行比较,另一个选择是以下(neater?)代码:

if total > 150 and form["customerProv"].value in {"BC", "bc", "Bc", "bC"}:

...检查该值是否在该集合中。

在您的特定情况下执行此操作的最佳方法是注意您正在与同一字符串的一堆不同的套接字版本进行比较。如果您将字符串转换为大写字母,那么您可以非常简单地比较它:

if total > 150 and form["customerProv"].value.upper() == "BC":

(由Jon Clements建议)