### Thread: Can I shorten this statement

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
2
Rep Power
0

#### Can I shorten this statement

if price1<=10 and price2<=10 and price3>10:
print "Non-taxable Subtotal: \$",(price1+price2)
elif price1>10 and price2<=10 and price3<=10:
print "Non-taxable Subtotal: \$",(price2+price3)
elif price1<=10 and price2>10 and price3<=10:
print "Non-taxable Subtotal: \$", (price1+price3)
elif price1<=10 and price2<=10 and price3<=10:
print "Non-taxable Subtotal: \$",(price1+price2+price3)
elif price1<=10:
print "Non-taxable Subtotal: \$",price1
elif price2<=10:
print"Non-taxable Subtotal: \$",price2
elif price3<=10:
print "Non-taxable Subtotal: \$",price3
else:
print "Non-taxable Subtotal: \$0.00"
2. #### Don't use if statements!

Don't know if you can shorten it. I can.
Ken Iverson taught us to use the True False result of conditional expressions as factors. Dr. Donald Knuth teaches (or taught if he no longer lectures) the notation in his classes.

Code:
```from numpy import dot
subtotal = dot(mask, (price1, price2, price3,))
print("Non-taxable Subtotal: \${}".format(subtotal))```

How do I know this is correct? What is my dot function?
Code:
```# solved in python3
# I have not tried to match the 0.00 case, because of the decimal point.
# Your printed display is unsatisfactory anyway.

try:
from numpy import dot
except:
def dot(a,b): # implemented for vectors only
assert len(a) == len(b)
return sum(A*B for (A,B,) in zip(a,b))

for i in range(8,12):
price1 = i
for j in range(8,12):
price2 = j
for k in range(8,12):
price3 = k

if price1<=10 and price2<=10 and price3>10:
print("Non-taxable Subtotal: \$"+str(price1+price2))
elif price1>10 and price2<=10 and price3<=10:
print("Non-taxable Subtotal: \$"+str(price2+price3))
elif price1<=10 and price2>10 and price3<=10:
print("Non-taxable Subtotal: \$"+str(price1+price3))
elif price1<=10 and price2<=10 and price3<=10:
print("Non-taxable Subtotal: \$"+str(price1+price2+price3))
elif price1<=10:
print("Non-taxable Subtotal: \$"+str(price1))
elif price2<=10:
print("Non-taxable Subtotal: \$"+str(price2))
elif price3<=10:
print("Non-taxable Subtotal: \$"+str(price3))
else:
print("Non-taxable Subtotal: \$0.00")

subtotal = dot(mask, (price1, price2, price3,))
print('Non-taxable Subtotal: \${}'.format(subtotal))

print('')```
if statements make programs more complex by increasing the number of paths through the code. Don't use them! For this program they are unneeded.

Can we compress it to one line? Yes. Doing so ruins the expository nature.
Last edited by b49P23TIvg; November 5th, 2013 at 05:46 AM.
3. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
May 2009
Posts
652
Rep Power
39
I think you can make it easier to understand by placing all statements under one another instead of the if and if statements.
Code:
```## "not accounted" for messages added for testing and can be deleted

total=0
if price1 <= 10:
if price2 <= 10:
if price3 > 10:
total = price1+price2
else:  ## price3 <= 10
total = price1+price2+price3
else:  ## price2 >
if price3 <= 10:
total = price1+price3
else:  ## price3 >
print "price 1 <.  price2 >, and price3 > not accounted for"
else :  ## price > 10
if price2 <= 10:
if price3 <= 10:
total = price2 + price3
else:
print "price1 >,  price2 <,  and price3 > not accounted for"
else:  ## price 2 >
print "price1 > and price2 > not accounted for"

if total > 0:
print "Non-taxable Subtotal: \$%f" % (total)
elif price1<=10:
print "Non-taxable Subtotal: \$",price1
elif price2<=10:
print"Non-taxable Subtotal: \$",price2
elif price3<=10:
print "Non-taxable Subtotal: \$",price3
else:
print "Non-taxable Subtotal: \$0.00"```
Last edited by dwblas; November 4th, 2013 at 09:40 PM.
4. #### see my prior post this thread for detail.

Code:
`print('Non-taxable Subtotal: \${:.2f}'.format(sum((p<=10)*p for p in (price1, price2, price3))))`
Last edited by b49P23TIvg; November 5th, 2013 at 11:56 AM. Reason: insert 2 place decimal format
5. #### shortest expressions I can find.

Code:
```#python
sum(p<=10and p for p in(price1,price2,price3)) # hard to read.
sum(p for p in(price1,price2,price3)if p<=10)  # for me, easiest to read.
sum(p*(p<=10)for p in(price1,price2,price3))   # shortest without major insight.

NB. in j,  www.jsoftware.com  The space characters and nota bene are not required.
([: +/ (* <:&10)) price1,price2,price3         NB. tacit.
+/ (* <:&10) price1,price2,price3              NB. explicit.

[prices =: |: 8+4(#.inv)i.4^3   NB. data produced by the python for loop test code
8 8  8  8 8 8  8  8  8  8  8  8  8  8  8  8 9 9  9  9 9 9  9  9  9  9  9  9  9  9  9  9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
8 8  8  8 9 9  9  9 10 10 10 10 11 11 11 11 8 8  8  8 9 9  9  9 10 10 10 10 11 11 11 11  8  8  8  8  9  9  9  9 10 10 10 10 11 11 11 11  8  8  8  8  9  9  9  9 10 10 10 10 11 11 11 11
8 9 10 11 8 9 10 11  8  9 10 11  8  9 10 11 8 9 10 11 8 9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11  8  9 10 11

([:+/(*<:&10))prices  NB. tacit
24 25 26 16 25 26 27 17 26 27 28 18 16 17 18 8 25 26 27 17 26 27 28 18 27 28 29 19 17 18 19 9 26 27 28 18 27 28 29 19 28 29 30 20 18 19 20 10 16 17 18 8 17 18 19 9 18 19 20 10 8 9 10 0

+/(*<:&10)prices      NB. explicit
24 25 26 16 25 26 27 17 26 27 28 18 16 17 18 8 25 26 27 17 26 27 28 18 27 28 29 19 17 18 19 9 26 27 28 18 27 28 29 19 28 29 30 20 18 19 20 10 16 17 18 8 17 18 19 9 18 19 20 10 8 9 10 0

whitespace: I'm sure the program is longer in white space.
Hard to tell though.```