jeudi 24 novembre 2016

Alternative Technique to Python "If...elif...else" to loop over 1,500 Options

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.

Aucun commentaire:

Enregistrer un commentaire