I was able to get the columns calculated but I just realized that the equation is different for some data (when the column "type" is either material/equipment)
Throw in another if loop to check the type to decide which equation to run:
...
#compute
extCost = int(qty) * float(unitCost)
if type == 'material':
unitPrice = (float(unitCost)/(100-15) * 100)
elif type == 'equipment':
unitPrice = (float(unitCost)/(100-20) * 100)
else:
#Your choice on what you would like this to do if type is not 'material' or 'equipment'.
...
Another option:
...
#compute
if type == 'material':
unitPriceModifier = 15
elif type == 'equipment':
unitPriceModifier = 20
else:
unitPriceModifier = #Replace with a default value here...
# Or throw an error/warning with unexpected type found
extCost = int(qty) * float(unitCost)
unitPrice = (float(unitCost)/(100-unitPriceModifier) * 100)
...
The option below assumes if the type is not 'material' it should always subtract 20. I’m not a fan of this method though as I’d rather explicitly check for each possible type value I care about and throw an error/warning if you get something you weren’t expecting (i.e. - randomly get a record with type == 'process'):
Yup. If your variable names get highlighted that means they're keywords and you are doing a bad
I’d rather explicitly check for each possible type value I care about and throw an error/warning if you get something you weren’t expecting (i.e. - randomly get a record with type == 'process' ):
dictionary.get(key, default_value) is great if you want a default value if a key doesn't exist. If you explicitly want to ensure the key exists, I would use the dictionary[key] syntax because it will raise a KeyError if that key isn't found
edit: this will cause a NameError when referencing unitPriceModifier later. If you're not looking for a default value maybe you're going for something like