1

Right now my python code looks like this:

if imput == "con": print "%.6f" % (con_getinfo["balance"]) elif imput == "xra": print "%.6f" % (xra_getinfo["balance"]) elif imput == "neg": print "%.6f" % (neg_getinfo["balance"]) elif imput == "grf": print "%.6f" % (grf_getinfo["balance"]) elif imput == "ninja": print "%.6f" % (ninja_getinfo["balance"]) 

Now i would like it to make it look less repetitive, like this:

if imput == con or imput == xra or imput == neg or imput == grf or imput == ninja: 

But i can't figure out how to assign to each of the conditions the proper reaction.

2 Answers 2

4

You can store references to the functions in a dictionary, then it's just a simple lookup:

response = { "con": con_getinfo, "neg": neg_getinfo, ... } print "%.6f" % (response[imput]["balance"]) 
Sign up to request clarification or add additional context in comments.

Comments

1

This is arguably a dupe, but in a nutshell... the pythonic way is to use a dictionary. There are probably better ways to refactor this, but to start:

mydict = {"con": con_getinfo, "xra": xra_getinfo, "neg": neg_getinfo, "grf": grf_getinfo, "ninja" : ninja_getinfo} lookup = mydict.get(imput) if lookup: #won't fail if imput isn't on of the options print "%.6f" % (lookup["balance"]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.