-3

I am trying to calculate the tax but whenever I try to execute the if statements, it says 'BC' not defined. The if statements regrding tax are near the end.Any help is appreciated. Heres my code: 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>"

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 Answers1

1

You're referencing variables BC, bc and so on:

if total > 150 and form["customerProv"].value == BC or bc or Bc or bC:

...but you've never told Python what they mean! So Python throws an error at you. Did you mean to use strings?

if total > 150 and form["customerProv"].value == "BC" or "bc" or "Bc" or "bC":

That will fail, too, though. This is because you can't chain or in Python like this. You should check equality separately for each value:

if total > 150 and form["customerProv"].value == "BC" or form["customerProv"].value == "bc" or form["customerProv"].value == "Bc" or form["customerProv"].value == "bC":

Another option is the following (neater?) code if you want to compare a value to a bunch of other values:

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

...which checks if the value is in that set.

The neatest way to do this in your particular case is to notice that you're comparing against a bunch of different cased versions of the same string. If you convert the string you're comparing to upper case, then you can compare it very simply:

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

(suggested by Jon Clements)

Community
  • 1
  • 1
Matthew
  • 2,232
  • 4
  • 23
  • 37
  • Which still wouldn't work, because `"BC" or "bc"` is always True. – Daniel Roseman Jul 31 '14 at 10:00
  • Yep, caught that one just after I posted the answer... an unfortunate oversight on my part! Been editing my post to fix it :D – Matthew Jul 31 '14 at 10:02
  • 4
    Could even just use `if total > 150 and form["customerProv"].value.upper() == 'BC':` instead of the set – Jon Clements Jul 31 '14 at 10:03
  • That's true, and much neater! I was operating under the idea that this was a more general sort of question on chaining comparisons, though, in which case the set is probably the neatest way to handle it. But I'll add that into my answer because it's a very neat way to do what he is doing. – Matthew Jul 31 '14 at 10:05
  • i got the python to be working but dont seem to be getting the right answers – user3893867 Jul 31 '14 at 10:08
  • Then perhaps your code's logic is wrong? That'd be outside the scope of this question, I suspect. – Matthew Jul 31 '14 at 10:09
  • yea trying to figure it out, its driving me nuts – user3893867 Jul 31 '14 at 10:11
  • At least your `if` statements work now! Don't forget to mark a best answer if this helped. – Matthew Jul 31 '14 at 10:12
  • matthew would you mind taking a look at my if statements and see if u can notice something? like for the second one i have total*0.12 but im not getting the right answer if i try a scenario for it. thanks – user3893867 Jul 31 '14 at 10:13
  • Alas, I don't know much about tax. This is probably a topic for a different question, though I don't know if StackOverflow is the right place to ask that. – Matthew Jul 31 '14 at 10:14