I have an issue with the use of functions and switcher in python: I have this production cost function:
def fcostoproduccion (X,periodo):
if X > 0:
switcher = {
1: 200 + 15 * X,
2: 100 + 20 * X,
3: 100 + 4 * (X ** (1 / 2)),
4: 100 + 3 * X,
5: 150 + 6 * X,
6: 200 + 12 * (X ** (1 / 2)),
7: 200 + 15 * X,
8: 100 + 10 * X,
9: 100 + 6 * (X ** (1 / 2)),
10: 200 + 5 * (X ** (1 / 2)),
11: 100 + 10 * X,
12: 150 + 6 * X
}
return
And at the end I'm trying to look for the value:
for l in range(j+1, k+1):
Ordenar = O[l]
Produccion = fcostoproduccion(Demanda, l)
I know I'm making a mistake but don't know how to solve it. Thanks in advance.
A couple of things with the function fcostoproduccion
:
None
periodo
is unsused in the function).It is hard to discern your intention with the function however I guess
X
X
hence the dictionary switcher
.Based on the above assumtions, you can modify the function fcostoproduccion
as follows:
def fcostoproduccion(X):
switcher = {
1: 200 + 15 * X,
2: 100 + 20 * X,
3: 100 + 4 * (X ** (1 / 2)),
4: 100 + 3 * X,
5: 150 + 6 * X,
6: 200 + 12 * (X ** (1 / 2)),
7: 200 + 15 * X,
8: 100 + 10 * X,
9: 100 + 6 * (X ** (1 / 2)),
10: 200 + 5 * (X ** (1 / 2)),
11: 100 + 10 * X,
12: 150 + 6 * X
}
return switcher[X] if X in switcher else None #Handle cases where X switcher is not defined for X
>>> [fcostoproduccion(i) for i in range(14)]
[None, 215, 140, 106.92820323027551, 112, 180, 229.39387691339815, 305, 180, 118.0, 215.81138830084188, 210, 222, None]