1

The following is a sample code of a community project I am undergoing.

cat1 = 'Engr, Bricklayer, Attendant'
cat2 = 'Programmer, Artist, Engr, Servant'
cat3 = 'Programmer, Typist'


amount = float(input("Enter amount: "))

choice = input("Select Beneficiary: ")


print("-----------------------------------")

if choice == cat1:
    print("Name: Engr\nShare: 1/6 (1 cikin 6)\nBenefits: N" + str((amount/6) * 1))
    print("-----------------------------------")
    print("Name: Bricklayer\nShare: 1/2 (3 cikin 6)\nBenefits: N" + str((amount/6) * 3))
    print("-----------------------------------")
    print("Name: Attendant\nShare: 1/3 (2 ciin 6)\nBenefits: N" + str((amount/6) * 2))
    print("-----------------------------------")
    print("Jimilla: N" + str(sum([(amount/6) * 1, (amount/6) * 3, (amount/6) * 2])))

elif choice == cat2:
    print("Name: Programmer\nShare: 1/8 (3 cikin 24)\nBenefits: N" + str((amount/24) * 3))
    print("-----------------------------------")
    print("Name: Artist\nShare: 1/6 (4 cikin 24)\nBenefits: N" + str((amount/24) * 4))
    print("-----------------------------------")
    print("Name: Engr\nShare: 1/6 (4 cikin 24)\nBenefits: N" + str((amount/24) * 4))
    print("-----------------------------------")
    print("Name: Servant\nShare: Ragowa (13 cikin 24)\nBenefits: N" + str((amount/24) * 13))
    print("-----------------------------------")
    print("Jimilla: N" + str(sum([(amount/24)*3,(amount/24)*4,(amount/24)*4,(amount/24)*13])))

Below is the output after receiving values from the user:

Enter amount: 2540000
Select Beneficiary: Engr, Bricklayer, Attendant
-----------------------------------
Name: Engr
Share: 1/6 (1 cikin 6)
Benefits: N423333.3333333333
-----------------------------------
Name: Bricklayer
Share: 1/2 (3 cikin 6)
Benefits: N1270000.0
-----------------------------------
Name: Attendant
Share: 1/3 (2 ciin 6)
Benefits: N846666.6666666666
-----------------------------------
Jimilla: N2540000.0
>>> 

As you can see, I am using the If...elif...else control statements to move through the elements in the categories defined at the top of the code. I have to create about 1,500 different categories with a combination of different elements/beneficiaries entitled to different shares. Is there any technique I can use to loop through the options instead of using If...elif...else?

I am new to Python. The output of the code above is what I want, but using the If...elif...else to check the user input against the 1,500 categories will be a bit monotonous or rather, hectic.

I need your help, please.

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
shallowGeek
  • 59
  • 1
  • 1
  • 10
  • dictionary is a good option. And make a uniqueness in your printing values and make it form of a function. – Rahul K P Nov 24 '16 at 12:06
  • Could http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python be helpful? – fredtantini Nov 24 '16 at 12:06
  • Build a dictionary of functions, constructing the strings to print. – Eli Korvigo Nov 24 '16 at 12:08
  • Thank you very much. Am really grateful. However, as I said, am new to python. A sample code will really assist me to figure out how to re-factor the code. Am grateful once more! – shallowGeek Nov 24 '16 at 13:59

1 Answers1

1

First, you could encode all of your beneficiaries as a CSV file in the format

[Beneficiary, Numerator, Dinominator]

as shown below

category.csv contents:

Engr, 1, 6
Bricklayer, 3, 6
Attendant, 2, 6
Programmer, 3, 24
Artist, 4, 24
Engr, 4, 24
Servant, 13, 24

Then convert the data to a dictionary and manipulate accordingly. The following code will give a similar output to yours.

import csv

beneMap = {}

with open("category.csv") as f:
    csvFile = csv.reader(f)
    for row in csvFile:
        beneMap[row[0]] = (row[1], row[2])

def getJimilla(beneMap, benef, amount):
    benList = benef.split(', ')
    jimilla = 0
    for name in benList:
        ben = beneMap[name]
        jtemp = amount*int(ben[0])/int(ben[1])
        print("Name: "+name 
              +"\nShare: "+ben[0]+"/"+ben[1]+" ("+ben[0]+" cikin "+ben[1]+")"
              +"\nBenefits: N" + str(round(jtemp,2))
              +"\n-----------------------------------")
        jimilla += jtemp
    print("Jimilla: N"+str(round(jimilla,2)))

amount = float(input("Enter amount: "))
choice = input("Select Beneficiary: ")

print("-----------------------------------")

getJimilla(beneMap, choice, amount)

Sample run:

Enter amount: 2540000
Select Beneficiary: Engr, Bricklayer, Attendant
-----------------------------------
Name: Engr
Share:  4/ 24 ( 4 cikin  24)
Benefits: N423333.33
-----------------------------------
Name: Bricklayer
Share:  3/ 6 ( 3 cikin  6)
Benefits: N1270000.0
-----------------------------------
Name: Attendant
Share:  2/ 6 ( 2 cikin  6)
Benefits: N846666.67
-----------------------------------
Jimilla: N2540000.0

Note that the category is understood when the input sequence is given so no extra information need to be carried in the CSV file regarding the category of the beneficiary.

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
  • I would move the _int_ conversion to the _beneMap_ filling, and use string formap for the printing... – Copperfield Nov 24 '16 at 17:54
  • You're right, @Copperfield. I initially thought of the same as it is *technically* the right thing to do to preserve the semantics. However, I left it as a string to focus on reducing the code length as the OP might want. Otherwise, you would see far more `str()` conversions. – Ébe Isaac Nov 24 '16 at 17:58
  • Thank you very much @ÉbeIsaac. This is exactly what I wanted. Though in the real implementation, the beneficiaries have different shares. For instance, one beneficiary can have 1/2, 2/3, 1/6, depending on the combination of beneficiaries selected. Like, if Programmer and Attendant are selected, Programmer's share will be 3/6, while that of the Artist 2/6. However, if alone, the Programmer can take the whole share etc. Also, there may be a case where a beneficiary can share the reminder with some other beneficiaries. I really appreciate your contribution. Am grateful! – shallowGeek Nov 25 '16 at 08:14
  • @shallowGeek You're welcome. This is just the base code; you may tailor the additional logic as needed. – Ébe Isaac Nov 25 '16 at 11:55