passing python arguments in different order along with its variable name -


im new python , raised little task of creating small script small criteria must met. first of all, original code works great. problem need how set code in way arguments can passed in order respective value (before running script). have checked, , theres no post specific question here am.

using python 3.5.1 windows 7 x64

here's situation:

the script require 4 variables (all floats). -var1, -var2, -var3, -var4

i want run script follows:

>> python test.py -var1 4 -var2 5.4 -var3 7 -var4 3.2 

but requirements says must able run script, variables order dont matter long value matches variable name on left. meaning...

>> python test.py -var3 7 -var1 4 -var4 4.2 -var2 5.4 

results should same running both scripts.

i cant post original code im working in, here's example of how need code behave. no matter order arguments passed when running script.

>     x = var1+var2 >     y = var3+var4 >     z = (x+y)/var4 >     print (str(x)) #result should 9.4 >     print (str(y)) #result should 10.2 >     print (str(z)) #result should 6.125 

any appreciated. thanks

the argparse module handle you

script.py --var4 2.3 --var2 1 --var1 11.234234 --var3 0

from argparse import argumentparser  parser = argumentparser() parser.add_argument('--var1', action='store', type=float) parser.add_argument('--var2', action='store', type=float) parser.add_argument('--var3', action='store', type=float) parser.add_argument('--var4', action='store', type=float) args = parser.parse_args()  var1 = args.var1 var2 = args.var2 var3 = args.var3 var4 = args.var4  x = var1 + var2 y = var3 + var4 z = (x+y) / var4 print (str(x)) #result should 9.4 print (str(y)) #result should 10.2 print (str(z)) #result should 6.125 

Comments