以下是我正在进行的社区项目的示例代码。
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])))
以下是从用户接收值后的输出:
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
>>>
如您所见,我使用If ... elif ... else控制语句来遍历代码顶部定义的类别中的元素。我必须创建大约1,500个不同的类别,其中包含不同元素/受益人的组合,可以享有不同的股份。有没有什么技术可以用来循环选项而不是使用If ... elif ... else?
我是Python新手。上面代码的输出是我想要的,但是使用If ... elif ... else来检查针对1,500个类别的用户输入将会有点单调或者说是忙乱。
我需要你的帮助。
答案 0 :(得分:1)
首先,您可以将所有受益人编码为
格式的CSV文件[受益人,分子,反对者]
如下图所示
category.csv
内容:
Engr, 1, 6
Bricklayer, 3, 6
Attendant, 2, 6
Programmer, 3, 24
Artist, 4, 24
Engr, 4, 24
Servant, 13, 24
然后将数据转换为字典并进行相应的操作。以下代码将为您提供类似的输出。
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)
示例运行:
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
请注意,在提供输入序列时,应了解该类别,因此CSV文件中不需要提供有关受益人类别的额外信息。