I am wondering how I can approach this; I have a function I've written with 4 inputs. However, I need to apply this function to many different combinations of 4 inputs (with the number of combinations being varied each time), and sum the outputs. I will attach part of my code, but for example, say my code takes arguments "chemical," "acres," "amountperacre," and "units." I would like to be able to simultaneously run the code for multiple chemicals (that are linked to the following 3 variables) at a time and write for the resulting "MTCO2e" to be summed so that I don't have to run the code 50 times, record, the output of each run, and then sum them all. How might I go about this/is this possible? I've been doing a lot of reading about multiple arguments but am having trouble/confusion applying what I've read. Right now my code (which I am still building) looks like this. It works for individual chemicals, but I don't know how to synthesize it into one joint code that can sum the result of the MTCO2e's. Thank you!!
def chemicalemissions(chemical, acres, amountperacre, units):
.
.
defining varibables, etc.
.
.
#Cayuse Plus info
#density units = lb/gal
cayuseplusdenisty = 10.26
activeingredientcayuseplus = .25
if chemical == 'Cayuse Plus':
densitykg = cayuseplusdenisty * lbstokg
#gives density in kg/gal
if units == 'gal':
densitykg = densitykg
#gives density in kg/fl oz
if units == 'fl oz':
densitykg = densitykg * galtofloz
#total kg of product applied
totalamountappliedkg = densitykg * amountperacre * acres
#total kg active ingredient
totalkgactiveingredient = totalamountappliedkg * activeingredientcayuseplus
#total MJ required
totalMJ = MJgeneral * totalkgactiveingredient
#MTCO2e
MTCO2e = totalMJ * kgCO2perMJperkgactiveingredient * kgCO2etoMTCO2e
print(MTCO2e)
#Tri-Fol info
#density units = lbs/gal
trifoldensity = 10.1
trifolactiveingredient = .25
if chemical == 'Tri-Fol':
densitykg = trifoldensity * lbstokg
#gives density in kg/gal
if units == 'gal':
densitykg = densitykg
#gives density in kg/fl oz
if units == 'fl oz':
densitykg = densitykg * galtofloz
#total kg of product applied
totalamountappliedkg = densitykg * amountperacre * acres
#total kg active ingredient
totalkgactiveingredient = totalamountappliedkg * trifolactiveingredient
#total MJ required
totalMJ = MJgeneral * totalkgactiveingredient
#MTCO2e
MTCO2e = totalMJ * kgCO2perMJperkgactiveingredient * kgCO2etoMTCO2e
As for what I've tried I've experimented with *arg, but it seems like that is meant to be for one variable type? Like for example, multiple integers. But I'm not sure I can apply it to the types of variables I have.
given the context that your provided in response to my comment and if this is not supposed to be quick and dirty it would probably be best to make a class structure for the chemicals like this rather than using a function with parameters.
here is how you might do it:
This format will allow you to extend the functionality of your (assumed) reporting application more easily than if it was all in a singular massive function.
This is just a minimal example - and there are many ways to improve upon what is shared here. Hopefully this gives you enough ideas to arrive at a better solution though.
If you do only need a quick and dirty solution: just continue as you have done but instead of ending the function with
print(MTCO2e)usereturn MTCO2eThen in your loop where you are calling
chemicalemissions()you just assign it to a variable likeresult = chemicalemissions().You can now manipulate the result as you like - including appending each one to a list and then
sum()ing the list after you have finished looping through the data.