我已经尝试
totalValue = 0 bagsChecked = 0
for x in range(len(volunteerList)):
record = volunteerList[x]
if record[1] == "1 pence":
amount = 1.00
elif record[1] == "2 pence":
amount = 1.00
elif record[1] == "5 pence":
amount = 5.00
elif record[1] == "10 pence":
amount = 5.00
elif record[1] == "20 pence":
amount = 10.00
elif record[1] == "50 pence":
amount = 10.00
elif record[1] == "1 pound":
amount = 20.00
else:
amount = 20.00
totalValue = totalValue + amount
bagsChecked = bagsChecked + 1
print("Bags checked: ",bagsChecked)
print("Total Value: ",totalValue)'''
有没有一种方法可以解决此问题,以便显示准确的托运行李数量和总价值。
答案 0 :(得分:0)
我不确定这将运行,因为我不知道您的数据是什么样子,但是您可以尝试执行以下操作:
total_value = 0
total_bags = 0
for rec in volunteerList:
if "pence" in rec[1]:
total_value += float(rec[1].split[' '][0])
if "pound" in rec[1]:
total_value += 20 * float(rec[1].split[' '][0])
total_bags += 1
print("Bags checked: {}".format(total_bags))
print("Total Value: {}".format(total_value))
这假设volunteerList
中的每个条目都是一个包含“便士”或“英镑”的字符串。然后,我们将字符串拆分并保留计数的数字部分。