To get straight to problem, I have following data.
A = [170,170,150]
b = 160
C = [2,2]
xb = [b/(k-1) for k in C]
print(xb)
Output:
[160.0, 160.0]
Now change C list:
A = [170,170,150]
b = 160
C = [2,1]
xb = [b/(k-1) for k in C]
print(xb)
Output:
Error ZeroDivisionError: division by zero
Okay expected not run, now we find a solution to run it.
xb = []
for k in C:
try:
xb.append(b/(k-1))
except ZeroDivisionError:
xb.append(0)
print(xb)
Output:
[160.0, 0]
My question is how could I write a shorter version of above solution such as first solution?