Python + HTML编程网站订单

时间:2014-07-31 02:17:55

标签: python html

您好我正在开展一个学校项目,我需要使用python和HTML为网站制作订单。我无法添加这些内容:

if form.getvalue("interior"):
    print "<p>Interior Painting(s) quantity:", form["quantity"].value, "</p>"
    print "<p>You Cost is :$" ,int(form["quantity"].value)*2, ".00</p>"

else:
        print "<p>Interior Painting(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

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

else:
        print "<p>Exterior Painting(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

if form.getvalue("pressure"):
    print "<p>Pressure Washing quantity:", form["quantity3"].value, "</p>"
    print "<p>You Cost is :$" ,int(form["quantity3"].value)*150, ".00</p>"
else:
       print "<p>Pressure Washings(s)ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

if form.getvalue("wood"):
    print "<p>Wood Finishing quantity:", form["quantity4"].value, "</p>"
    print "<p>You Cost is $" ,int(form["quantity4"].value)*3, ".00</p>"
else:
        print "<p>Wood Finsihings(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

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

我正在尝试将所有int(形式[quantity..1..2] .value)加在一起并在这些之后显示结果。我尝试将表单[quantity] .value分配给变量并添加它们虽然getvalue仅在用户选择该选项时执行,但是我怎么能这样做呢?谢谢。

完整代码

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

if form.getvalue("interior"):
    print "<p>Interior Painting(s) quantity:", form["quantity"].value, "</p>"
    print "<p>You Cost is :$" ,int(form["quantity"].value)*2, ".00</p>"

else:
        print "<p>Interior Painting(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

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

else:
        print "<p>Exterior Painting(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

if form.getvalue("pressure"):
    print "<p>Pressure Washing quantity:", form["quantity3"].value, "</p>"
    print "<p>You Cost is :$" ,int(form["quantity3"].value)*150, ".00</p>"
else:
       print "<p>Pressure Washings(s)ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

if form.getvalue("wood"):
    print "<p>Wood Finishing quantity:", form["quantity4"].value, "</p>"
    print "<p>You Cost is $" ,int(form["quantity4"].value)*3, ".00</p>"
else:
        print "<p>Wood Finsihings(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

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

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

print "</body></html>"

1 个答案:

答案 0 :(得分:0)

这是我对你的问题的看法,逻辑是将计算值分配给变量,这样你就可以操纵它们来获得总和。

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(s) quantity:", form["quantity"].value, "</p>"
    print "<p>You Cost is :$" ,q1, ".00</p>"

else:
    q1 = 0
    print "<p>Interior Painting(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

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

else:
    q2 = 0
    print "<p>Exterior Painting(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

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

if form.getvalue("wood"):
    print "<p>Wood Finishing quantity:", form["quantity4"].value, "</p>"
    print "<p>You Cost is $" ,q4, ".00</p>"
else:
    q4 = 0
    print "<p>Wood Finsihings(s) ordered: <b>0</b> <br /> Cost: <b>$0</b></p>"

if form.getvalue("spraycan"):
    print "<p>Spray Can quantity:", form["quantity5"].value, "</p>"
    print "<p>You Cost is $" ,q5, ".00</p>"
else:
    q5 = 0
    print "<p>Spray Cans ordered: <b>0</b> <br /> Cost: <b>$0</b></p>" 

total = q1 + q2 + q3 + q4 + q5
print "<p>Total: ", total, "</p>"